I create a QPushButton instance before ui->setupUi(this), And this button wont clicked
-
as title says, in Qt6.6.1, I add a private (QPushButton *) member in MainWindow and created in the construct list.
code is shown below:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_button(new QPushButton("KSKSKSKSK", this)) { ui->setupUi(this); qDebug() << "object: "<< this <<"is created!"; // LetsBegin(); m_button->setGeometry(10,20,300,100); QObject::connect(m_button, &QPushButton::clicked,qApp, &QApplication::quit); }however when i run this, this button cant be clicked, and no apparence changing even when cursor move through.
by search some other website, i find that
ui->setupUi(this)sort of "cover" those QWidget created before this lineI wonder if there is any way to "re_activate" this
m_button?
Or i have to create this instance afterui->setupUi(this)anyway? -
I changed ths init method by using
m_button( (ui->setupUi(this), new QPushButton("KSKSKSKSK", this))), this seems worked(mainly becase i declearm_buttonafterui)
but still not a solution since this way only allow me to use construct list rather than create instance beforeui->setupUi(this) -
I changed ths init method by using
m_button( (ui->setupUi(this), new QPushButton("KSKSKSKSK", this))), this seems worked(mainly becase i declearm_buttonafterui)
but still not a solution since this way only allow me to use construct list rather than create instance beforeui->setupUi(this)@NULL_NULL
m_buttonis directly parented toMainWindow, which at this point doesn't have a central widget. That's added (amongst other things) insetupUi(). Hard to say where and why exactly things go wrong, but it's somewhere there.It'll be better to construct the button without a parent in the first place:
, m_button(new QPushButton("KSKSKSKSK"))You can add it after calling
setupUi(), e.g. withm_button->setParent(centralWidget());or by adding it to a layout with
ui->anyLayout->addWidget(m_button); -
S SGaist moved this topic from Qt in Education on
-
as title says, in Qt6.6.1, I add a private (QPushButton *) member in MainWindow and created in the construct list.
code is shown below:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_button(new QPushButton("KSKSKSKSK", this)) { ui->setupUi(this); qDebug() << "object: "<< this <<"is created!"; // LetsBegin(); m_button->setGeometry(10,20,300,100); QObject::connect(m_button, &QPushButton::clicked,qApp, &QApplication::quit); }however when i run this, this button cant be clicked, and no apparence changing even when cursor move through.
by search some other website, i find that
ui->setupUi(this)sort of "cover" those QWidget created before this lineI wonder if there is any way to "re_activate" this
m_button?
Or i have to create this instance afterui->setupUi(this)anyway?This post is deleted!