Signals and Slots when loading a ui file



  • Hello friends,

    I have an issue in my application. I'm trying to make an application composed by a MainWindow with a menu bar and a dynamic loaded central widget via UI Tools.

    The widget loads properly, but the events in it seems are not responding. I attach the source code of the involved files:

    mainWindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtUiTools>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_actionQuit_triggered();

    void on_actionFCA_Concept_Lattice_Builder_triggered();
    

    private:
    Ui::MainWindow *ui;
    QUiLoader loader;
    };

    #endif // MAINWINDOW_H
    @

    mainWindow.cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "maclbuilderform.h"
    #include <QVBoxLayout>
    #include <QPushButton>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_actionQuit_triggered()
    {
    QApplication::quit();
    }

    void MainWindow::on_actionFCA_Concept_Lattice_Builder_triggered()
    {
    QFile file(":/forms/maclbuilderform.ui");
    file.open(QFile::ReadOnly);
    QWidget *myWidget = loader.load(&file, this);
    file.close();
    this->setCentralWidget(myWidget);
    }
    @

    maclBuilderForm.h

    @#ifndef MACLBUILDERFORM_H
    #define MACLBUILDERFORM_H

    #include "ui_maclbuilderform.h"

    class MaclBuilderForm : public QWidget, private Ui::MaclBuilderForm
    {
    Q_OBJECT

    public:
    explicit MaclBuilderForm(QWidget *parent = 0);

    protected:
    void changeEvent(QEvent *e);
    public slots:
    void on_attrNewButton_clicked();
    };

    #endif // MACLBUILDERFORM_H@

    maclBuilderForm.cpp

    @#include "maclbuilderform.h"
    #include <iostream>

    MaclBuilderForm::MaclBuilderForm(QWidget *parent) :
    QWidget(parent)
    {
    setupUi(this);
    connect(attrNewButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    }

    void MaclBuilderForm::changeEvent(QEvent *e)
    {
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
    retranslateUi(this);
    break;
    default:
    break;
    }
    }

    void MaclBuilderForm::on_attrNewButton_clicked()
    {
    QApplication::quit();
    }
    @

    In this case, when you click the attrNewButton, the event clicked does not trigger.

    Thanks for all help.


  • Moderators

    Hi, welcome to devnet.

    I don't think you understood how QUiLoader works. It loads a widget from .ui file. Period. It doesn't have a built-in C++ compiler and even if it did it doesn't know that that particular .ui file has anything to do with MaclBuilderForm class which header you happened to include in your app.

    Since you have the class source and ui file why do you even need a QUiLoader? You could simply do this:
    @
    auto myWidget = new MaclBuilderForm();
    setCentralWidget(myWidget);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.