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. [SOLVED] calling a function outside of the dialog class
QtWS25 Last Chance

[SOLVED] calling a function outside of the dialog class

Scheduled Pinned Locked Moved General and Desktop
20 Posts 3 Posters 7.7k Views
  • 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.
  • M Offline
    M Offline
    mlong
    wrote on last edited by
    #4

    How did you integrate the concepts in the code above into your own code?

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

      loginbox.h
      @#ifndef LOGINBOX_H
      #define LOGINBOX_H
      #include <QTcpSocket>
      #include <QDialog>
      #include "mainwindow.h"
      #include "ui_loginbox.h"
      namespace Ui {
      class loginBox;
      }

      class loginBox : public QDialog
      {
      Q_OBJECT

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

      loginBox(MainWindow *w)
      {
       //   connect(this,SIGNAL(click()),w,SLOT(clicked()));
      }
      

      signals:
      void click();

      private slots:
      void on_loginButton_clicked();

      private:
      Ui::loginBox *ui;
      // This is the socket that will let us communitate with the server.
      QTcpSocket *socket;

      };

      #endif // LOGINBOX_H
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #6

        You're creating an additional constructor which takes a MainWindow * as a parameter in order to make your connection. As such, the things in your default constructor (the one that takes a QWidget *) aren't getting called... those things would include instantiating ui and calling ui->setupUi(this).

        Do you need some extra help understanding multiple constructors? It's a pretty fundamental C++ thing, and if you're not clear on it, speak up and we'll give you some guidance. :-)

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #7

          A quick fix, by the way, would be to get rid of the lines 19-22 above and add the following to your default constructor:
          @
          loginBox::loginBox(QWidget *parent)
          {
          // The stuff you already have stays here ...

          // ONLY ADD THE FOLLOWING LINES (DON'T MAKE ANY OTHER CHANGES!)

          // See if the parent is a MainWindow *
          MainWindow *w = qobject_cast<MainWindow *> parent;
          
          if (w)  // if it is, make the connection
          {
              connect(this, SIGNAL(click()), w, SLOT(clicked()));
          }
          

          // END OF THE STUFF TO ADD
          }
          @

          in your on_loginButton_clicked() method be sure you call
          @
          emit click();
          @

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

            sure, i will take your advice on constructors mlong.

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

              i am getting an error... 15: error: cannot resolve overloaded function 'qobject_cast' based on conversion to type 'MainWindow*'. 15: error: expected ',' or ';' before 'parent'
              @loginBox::loginBox(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::loginBox)
              {
              ui->setupUi(this);

              // See if the parent is a MainWindow *
              MainWindow *w = qobject_cast<MainWindow *> parent;
              
              if (w)  // if it is, make the connection
              {
                  connect(this, SIGNAL(click()), w, SLOT(clicked()));
              }
              

              }@

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #10

                Actually, an even better fix would be to make the connection inside of your mainwindow class.

                Get rid of the stuff I suggested before :-)

                Then in your mainwindow class, right after you have
                @
                login = new loginBox(this);
                @
                add
                @
                connect(login, SIGNAL(click()), this, SLOT(clicked()));
                @

                That way your dialog doesn't have to know about the mainwindow to make have the connection made.

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                  is the above code suppose to go to the clicked function as soon as the dialog window is closed? because its not working.

                  i have void clicked(); in the mainwindow.h

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on last edited by
                    #12

                    I'm assuming it's supposed to be called when you click the login button on the dialog. That's what I read from your code you posted.

                    Without a better synopsis of what you're trying to accomplish, it's very hard to figure out. There's a number of threads which are going simultaneously covering a number of issues.

                    I think that perhaps there's a fundamental flaw in the design and logic of what's going on in the app, so it makes it kind of a hard moving target.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                      yes, its supposed to be called when the user clicks the login button on the dialog. i have verified that the clicked function never gets called.

                      here is the mainwindow.cpp
                      @#include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include "loginbox.h"
                      #include <QRegExp>
                      #include <QSettings>
                      //#include <QTcpSocket>

                      QString serverLineEdit;
                      QString userLineEdit;
                      int connect1 = 0;

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

                      login->setModal(true);
                      

                      login->show();
                      connect(login, SIGNAL(click()), this, SLOT(clicked()));

                      }

                      void MainWindow::clicked(){
                      QSettings settings("config.ini", QSettings::IniFormat);
                      serverLineEdit = settings.value("serverLineEdit").toString();
                      userLineEdit = settings.value("userLineEdit").toString();
                      connect1 = settings.value("ConnectToServer").toInt();
                      socket = new QTcpSocket(this);

                          this->connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
                          this->connect(socket, SIGNAL(connected()), this, SLOT(connected()));
                      
                          socket->connectToHost(serverLineEdit, 4200);
                      

                      }
                      @

                      and the dialog.cpp
                      @#include "loginbox.h"
                      #include "ui_loginbox.h"
                      #include "mainwindow.h"
                      #include <QRegExp>
                      #include <QSettings>
                      int connect2;

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

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

                      void loginBox::on_loginButton_clicked()
                      {
                      connect2 = 1;
                      //save the result of serverLineEdit to the config.ini file
                      QSettings settings("config.ini", QSettings::IniFormat);
                      settings.setValue("serverLineEdit", ui->serverLineEdit->text());
                      settings.setValue("userLineEdit", ui->userLineEdit->text());
                      settings.setValue("ConnectToServer", connect2);
                      this->close();

                      }@

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mlong
                        wrote on last edited by
                        #14

                        Overall design issues notwithstanding...

                        Make the connect immediately after the "new loginBox(this)". (Move it from line 22 to line 19 above)

                        But much more importantly, you also have to actually emit your click() signal in the dialog box. That needs to happen between lines 27 and 28 in dialog.cpp above.

                        Software Engineer
                        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                          can i have an example on how to emit the click() signal in the dialog.cpp file?

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mlong
                            wrote on last edited by
                            #16

                            @
                            emit click();
                            @

                            Software Engineer
                            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mlong
                              wrote on last edited by
                              #17

                              If you're not intimately familiar with signals and slots, I highly recommend reading "this":http://doc.qt.nokia.com/4.7-snapshot/signalsandslots.html overview. Please. :)

                              Software Engineer
                              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                                thanks a bunch mlong. the code works great now :)
                                yes, i will read signals and slots

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mlong
                                  wrote on last edited by
                                  #19

                                  Glad it's working!

                                  Software Engineer
                                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                                    also, i am starting to understand what can and can't be done inside of a constructor.

                                    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