如何添加一个小部件(例如QPushButton)动态到设计器中内置的布局 [英] How to add a widget (QPushButton for example) dynamically to a layout built in designer
问题描述
我正在玩一个Qt主要寻找重写一个旧的java应用程序symbian,我有我的自我有点困惑。
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.
我应该先的所有解释,C ++不是我的功夫,这可能是问题的原因。
I should first of all explain that C++ is not my kung-fu, and that may be the cause of the problem.
我想做的是添加一个简单的QPushButton到
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:
错误:从'QPushButton *'
转换为非标量类型'QPushButton'
请求
/home/graham/myFirstApp/mainwindow.cpp:22: error: conversion from ‘QPushButton*’ to non-scalar type ‘QPushButton’ requested
/home/graham/myFirstApp/mainwindow.cpp:27:
错误:调用
到$ b没有匹配函数$ b'QVBoxLayout :: addWidget(QPushButton&)'
/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:candidate are:void
QBoxLayout :: addWidget(QWidget *,int,
Qt :: Alignment)
/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.
尊敬
Graham。
推荐答案
是一个只是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.
QPushButton* button = new QPushButton();
button->setText(text);
ui->myLayout->addWidget(button);
这篇关于如何添加一个小部件(例如QPushButton)动态到设计器中内置的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!