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. QObject::connect question
Forum Updated to NodeBB v4.3 + New Features

QObject::connect question

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 7.5k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chuck Gao
            wrote on last edited by
            #5

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

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

            Chuck

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chuck Gao
                wrote on last edited by
                #7

                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

                Chuck

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Chuck Gao
                    wrote on last edited by
                    #9

                    [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

                    Chuck

                    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