Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signals and Slots when loading a ui file
Forum Updated to NodeBB v4.3 + New Features

Signals and Slots when loading a ui file

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 757 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valen46
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved