Solved Second ui slots not working
-
Hi @Ucn_
I doubt anyone can help you if you do not post your code, here as well.
Because from what you describe, it could be anything...
-
@J-Hilk thanks. I have updated my code. I hope someone can help
-
-
@Pablo-J-Rogina I posted the code, the private slot (void on_pushButton5_clicked()) does not work or even a manual one, the code I have posted is just structure of my code, I want to achieve the same results, it doesn't give errors, but the issue is the same
-
@Ucn_ said in Second ui slots not working:
I posted the code
Again, where?
Are you able to see the code under this topic thread?
I cannot. -
@Pablo-J-Rogina I am not sure why the code is not showing up, but it's there. I am posting here again
mainwindow.h#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
secnui.h
#ifndef SECNUI_H #define SECNUI_H #include "ui_secndui.h" #include <QObject> #include <QWidget> #include <QtWidgets> class secnUi : public QWidget { Q_OBJECT public: explicit secnUi(QWidget *parent = nullptr); QVBoxLayout *getTreeto(){ return ui2.verticalLayout_6; } signals: private slots: void on_pushButton5_clicked(); private: Ui::secnUi ui2; }; #endif // SECNUI_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "secnui.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { secnUi captureWig; QVBoxLayout *sendWig = new QVBoxLayout; sendWig = captureWig.getTreeto(); QWidget *w = new QWidget(); w->setLayout(sendWig); ui->tabWidget->addTab(w, "Newtab"); }
secnui.cpp
#include "secnui.h" #include "QDebug" secnUi::secnUi(QWidget *parent) : QWidget(parent) { ui2.setupUi(this); } void secnUi::on_pushButton5_clicked() { qDebug() << "test"; ui2.pushButton_02->setText("test"); }
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>657</width> <height>663</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QTabWidget" name="tabWidget"/> </item> <item> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>add</string> </property> </widget> </item> </layout> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>657</width> <height>21</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
secndui.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>secnUi</class> <widget class="QWidget" name="secnUi"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>426</width> <height>484</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QWidget" name="verticalLayoutWidget_3"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>421</width> <height>481</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout_6"> <item> <widget class="QGroupBox" name="groupBox_4"> <property name="title"> <string>GroupBox</string> </property> <layout class="QVBoxLayout" name="verticalLayout_7"> <item> <widget class="QGroupBox" name="groupBox_5"> <property name="title"> <string>GroupBox</string> </property> <layout class="QHBoxLayout" name="horizontalLayout_2"> <item> <widget class="QPushButton" name="pushButton5"> <property name="text"> <string>PushButton</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pushButton_02"> <property name="text"> <string>PushButton</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pushButton_3"> <property name="text"> <string>PushButton</string> </property> </widget> </item> </layout> </widget> </item> <item> <widget class="QLabel" name="label"> <property name="text"> <string>Recent Added:</string> </property> </widget> </item> <item> <widget class="QTableWidget" name="tableWidget"/> </item> <item> <widget class="QDialogButtonBox" name="buttonBox_3"> <property name="standardButtons"> <set>QDialogButtonBox::Ok</set> </property> </widget> </item> </layout> </widget> </item> </layout> </widget> </widget> <resources/> <connections/> </ui>
-
@Ucn_ said in Second ui slots not working:
secnUi captureWig;
This is a local variable which is destroyed as soon as MainWindow::on_pushButton_clicked() finishes.
Allocate captureWig on the heap. -
@jsulm thanks, secnUi *captureWig; captureWig = new secnUi(); fixed it. By the way do you know how can I connect a slot to this button that is in second ui, considering it is inside a loop and invoked in the mainwindow?
for (char letter = 'A'; letter <= 'M'; ++letter) { std::string s; s += letter; QString qstr = QString::fromStdString(s); QPushButton *button_first = new QPushButton(qstr); ui2.horizontalLayout->addWidget(button_first); }
a slot like this:
connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);
-
@Ucn_ said in Second ui slots not working:
connect(button_first, &QPushButton::triggered, this, &secnUi::openInNewWindow);
Sorry, you have to explain better: where is all this code located? Is "this" secnUi instance?
By the way this is not a slot but a call to connect(). -
@jsulm Actually I figured out, I only needed your solution "Allocate captureWig on the heap". Once again thanks.
-
Hi @Ucn_,
Just for your information:
std::string s;
s += letter;
QString qstr = QString::fromStdString(s);can be done better with this constructor:
QString qstr = QString(QChar(letter));
Regards
-
@aha_1980 thanks, I just didn't know how to convert that.