July 29, 2011

kalster kalster
Lab Rat
315 posts

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?

  1. void MainWindow::on_pushButton_clicked()
  2. {
  3.     QObject::connect(&ui;->pushButton, SIGNAL(clicked()), &a, SLOT(quit()));
  4.     quit.show();
  5. }

here is the mainwindow.cpp code.

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9. }
  10.  
  11. MainWindow::~MainWindow()
  12. {
  13.     delete ui;
  14. }
  15.  
  16. void MainWindow::on_pushButton_clicked()
  17. {
  18.     QObject::connect(&quit;, SIGNAL(clicked()), &a, SLOT(quit()));
  19.     quit.show();
  20.  
  21. }

8 replies

July 29, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi,

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

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

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

July 29, 2011

kalster kalster
Lab Rat
315 posts

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

July 29, 2011

Andre Andre
Area 51 Engineer
6031 posts

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?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 29, 2011

Chuck Gao Chuck Gao
Lab Rat
342 posts

In

  1. QObject::connect(&ui;->pushButton, SIGNAL(clicked()), qApp, SLOT(quit()));

“&ui;->pushButton”, a wrong “;” there.

 Signature 

Chuck

July 29, 2011

kalster kalster
Lab Rat
315 posts

i solved it.

I had to change…

  1. &ui;->pushButton

to…
  1. 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

July 29, 2011

Chuck Gao Chuck Gao
Lab Rat
342 posts

All right, you can also take a look at this one New Signal&Slot; syntax [developer.qt.nokia.com]

 Signature 

Chuck

July 29, 2011

Andre Andre
Area 51 Engineer
6031 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 29, 2011

Chuck Gao Chuck Gao
Lab Rat
342 posts
Andre wrote:
That new syntax is not going to help anybody as long as it is not in any released Qt version.

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

 Signature 

Chuck

 
  ‹‹ compiling taglib on Qt      Regarding registering QT dll using regsvr32 ››

You must log in to post a reply. Not a member yet? Register here!