如何将小部件(例如 QPushButton)动态添加到设计器内置的布局中
我正在尝试 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 技术交流群。

这只是一个C++问题,当您使用new-operator时,您需要使用星号将按钮声明为指针。
This is a merely C++ problem, you need to use asterisk to declare the button as pointer when you use new-operator.