QObject::connect question



  • I was reading a beginners tutor on a push button that closes then a user clicks it. in qt creator I created a new qt gui application using the default file names. I then went to the form designer and put on the main window a push button.

    in the mainwindow.cpp, I created the following code to put into the push button click function. The problem is that in the code &a, the "a" is defined in the main.cpp file only. I would like to close the application from the mainwindow.cpp instead but i don't know how or even if that is possible. I would like to close it in the mainwindow.cpp because that way i could do my from design in the designer, select my signal and then do the code all within the mainwindow.cpp. is that possible to do?
    @void MainWindow::on_pushButton_clicked()
    {
    QObject::connect(&ui;->pushButton, SIGNAL(clicked()), &a, SLOT(quit()));
    quit.show();
    }@

    here is the mainwindow.cpp code.
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

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

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

    void MainWindow::on_pushButton_clicked()
    {
    QObject::connect(&quit;, SIGNAL(clicked()), &a, SLOT(quit()));
    quit.show();

    }@



  • Hi,

    QApplication has a global variable called qApp, which you should use here:

    @
    void MainWindow::on_pushButton_clicked()
    {
    QObject::connect(&ui;->pushButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    quit.show();
    }
    @



  • the code does not work for me. any other suggestions?



  • You will need to give us more than "does not work". What did not work? Did you get errors compiling? Linking? Output on the terminal when your run?



  • In
    @
    QObject::connect(&ui;->pushButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    @

    "&ui;->pushButton", a wrong ";" there.



  • i solved it.

    I had to change...
    @&ui;->pushButton@
    to...
    @ui;->pushButton@
    thank you for the help.

    ps. yes i know about the ";". for some reason that is added in when i put "@" "@" around the code



  • All right, you can also take a look at this one "New Signal&Slot; syntax":http://developer.qt.nokia.com/wiki/New_Signal_Slot_Syntax



  • That new syntax is not going to help anybody as long as it is not in any released Qt version.



  • [quote author="Andre" date="1311919259"]That new syntax is not going to help anybody as long as it is not in any released Qt version.[/quote]

    Yes, but it's still a good way/article to learn Qt :D


Log in to reply
 

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