如何将小部件(例如 QPushButton)动态添加到设计器内置的布局中

发布于 10-13 09:46 字数 889 浏览 6 评论 0原文

我正在尝试 Qt,主要是想为 symbian 重写一个旧的 java 应用程序,但我自己有点困惑。

我首先要说明的是,C++不是我的功夫,这可能就是问题的根源。

我想做的是将一个简单的 QPushButton 添加到主窗口中的垂直布局,该窗口已在运行时在 qt 设计器中构建。

我的示例代码是这样的...

QPushButton button = new QPushButton();

QString text("Testing Buttons");

button.setText(text);

//How do we add children to this widget??

ui->myLayout->addWidget(button);

我收到的错误如下...

/home/graham/myFirstApp/mainwindow.cpp:22: 错误:从“QPushButton*”转换 到非标量类型“QPushButton” 已请求

/home/graham/myFirstApp/mainwindow.cpp:27: 错误:没有匹配的调用函数 到 'QVBoxLayout::addWidget(QPushButton&)'

/home/graham/myFirstApp/../qtsdk-2010.05/qt/include/QtGui/qboxlayout.h:85:候选者是:void QBoxLayout::addWidget(QWidget*, int, Qt::对齐)

现在我知道第一个错误与指针有关,但我不知道是什么,如果有人能够消除我的困惑并提供示例代码,那就太好了。

问候

格雷厄姆。

I'm having a play around with Qt mainly looking to rewrite an old java app for symbian and I have got my self a bit confused.

I should first of all explain that C++ is not my kung-fu, and that may be the cause of the problem.

What I am trying to do is add a simple QPushButton to a Vertical Layout in a main window which has been built in qt designer at run time.

My example code is something like this...

QPushButton button = new QPushButton();

QString text("Testing Buttons");

button.setText(text);

//How do we add children to this widget??

ui->myLayout->addWidget(button);

The errors I am getting are as follows...

/home/graham/myFirstApp/mainwindow.cpp:22:
error: conversion from ‘QPushButton*’
to non-scalar type ‘QPushButton’
requested

/home/graham/myFirstApp/mainwindow.cpp:27:
error: no matching function for call
to
‘QVBoxLayout::addWidget(QPushButton&)’

/home/graham/myFirstApp/../qtsdk-2010.05/qt/include/QtGui/qboxlayout.h:85: candidates are: void
QBoxLayout::addWidget(QWidget*, int,
Qt::Alignment)

Now I know the first error has something to do with pointers but I don't know what, if anyone is able to clear up my confusion and provide example code that would be great.

Regards

Graham.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-残月青衣踏尘吟2024-10-20 09:46:25

这只是一个C++问题,当您使用new-operator时,您需要使用星号将按钮声明为指针。

QPushButton* button = new QPushButton();
button->setText(text);
ui->myLayout->addWidget(button);

This is a merely C++ problem, you need to use asterisk to declare the button as pointer when you use new-operator.

QPushButton* button = new QPushButton();
button->setText(text);
ui->myLayout->addWidget(button);
旧时浪漫2024-10-20 09:46:25

QPushButton button = new QPushButton();

指向 QPushButton 的指针不是 QPushButton。这就是你的编译器所抱怨的,这就是你的问题。

QPushButton button = new QPushButton();

A pointer to QPushButton is not a QPushButton. That's what your compiler's bitching about and that's your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文