简体   繁体   English

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

[英]How to add a widget (QPushButton for example) dynamically to a layout built in designer

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. 我正在玩Qt,主要是为了重写Symbian的旧java应用程序,我让自己有点困惑。

I should first of all explain that C++ is not my kung-fu, and that may be the cause of the problem. 我首先应该解释一下C ++不是我的功夫,这可能是问题的原因。

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. 我想要做的是在主窗口中的一个垂直布局中添加一个简单的QPushButton,它在运行时在qt设计器中构建。

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:22:错误:从'QPushButton *'转换为非标量类型'QPushButton'请求

/home/graham/myFirstApp/mainwindow.cpp:27: error: no matching function for call to 'QVBoxLayout::addWidget(QPushButton&)' /home/graham/myFirstApp/mainwindow.cpp:27:错误:没有匹配函数来调用'QVBoxLayout :: addWidget(QPushButton&)'

/home/graham/myFirstApp/../qtsdk-2010.05/qt/include/QtGui/qboxlayout.h:85: candidates are: void QBoxLayout::addWidget(QWidget*, int, Qt::Alignment) /home/graham/myFirstApp/../qtsdk-2010.05/qt/include/QtGui/qboxlayout.h:85:候选人是: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. 格雷厄姆。

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

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

QPushButton button = new QPushButton();

A pointer to QPushButton is not a QPushButton. 指向QPushButton的指针不是QPushButton。 That's what your compiler's bitching about and that's your problem. 这就是你的编译器喋喋不休,这就是你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM