August 13, 2011

kalster kalster
Lab Rat
233 posts

[SOLVED] calling a function outside of the dialog class

Page  
1

I have created a mainwindow::clicked() function in the mainwindow. I would like to call that function from the dialog. is that possible? could i have a working example please? thank you in advanced

19 replies

August 13, 2011

loladiro loladiro
Lab Rat
594 posts

Not a working example, but a way to go

  1. class MainWindow : public QMainWindow
  2. {
  3.     //more suff
  4. public slots:
  5.     void clicked();
  6. }
  7. class Dialog : public QDialog
  8. {
  9.  
  10. public:
  11.     Dialog(MainWindow *w)
  12.     {
  13.         connect(this,SIGNAL(click()),w,SLOT(clicked()));
  14.     }
  15. signals:
  16.     void click();
  17. }

August 14, 2011

kalster kalster
Lab Rat
233 posts

i got the code working put now all my dialog widgets have disappeared.

August 14, 2011

mlong mlong
Robot Herder
1537 posts

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

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 14, 2011

kalster kalster
Lab Rat
233 posts

loginbox.h

  1. #ifndef LOGINBOX_H
  2. #define LOGINBOX_H
  3. #include <QTcpSocket>
  4. #include <QDialog>
  5. #include "mainwindow.h"
  6. #include "ui_loginbox.h"
  7. namespace Ui {
  8.     class loginBox;
  9. }
  10.  
  11. class loginBox : public QDialog
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     explicit loginBox(QWidget *parent = 0);
  17.     ~loginBox();
  18.  
  19.     loginBox(MainWindow *w)
  20.     {
  21.      //   connect(this,SIGNAL(click()),w,SLOT(clicked()));
  22.     }
  23. signals:
  24.     void click();
  25.  
  26. private slots:
  27.     void on_loginButton_clicked();
  28.  
  29. private:
  30.     Ui::loginBox *ui;
  31.     // This is the socket that will let us communitate with the server.
  32.     QTcpSocket *socket;
  33.  
  34. };
  35.  
  36. #endif // LOGINBOX_H

August 14, 2011

mlong mlong
Robot Herder
1537 posts

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. :-)

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 14, 2011

mlong mlong
Robot Herder
1537 posts

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:

  1. loginBox::loginBox(QWidget *parent)
  2. {
  3.     // The stuff you already have stays here ...
  4.  
  5. // ONLY ADD THE FOLLOWING LINES (DON'T MAKE ANY OTHER CHANGES!)  
  6.  
  7.     // See if the parent is a MainWindow *
  8.     MainWindow *w = qobject_cast<MainWindow *> parent;
  9.  
  10.     if (w)  // if it is, make the connection
  11.     {
  12.         connect(this, SIGNAL(click()), w, SLOT(clicked()));
  13.     }
  14. // END OF THE STUFF TO ADD
  15. }

in your on_loginButton_clicked() method be sure you call

  1.     emit click();

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 14, 2011

kalster kalster
Lab Rat
233 posts

sure, i will take your advice on constructors mlong.

August 14, 2011

kalster kalster
Lab Rat
233 posts

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’

  1. loginBox::loginBox(QWidget *parent) :
  2.     QDialog(parent),
  3.     ui(new Ui::loginBox)
  4. {
  5.     ui->setupUi(this);
  6.  
  7.     // See if the parent is a MainWindow *
  8.     MainWindow *w = qobject_cast<MainWindow *> parent;
  9.  
  10.     if (w)  // if it is, make the connection
  11.     {
  12.         connect(this, SIGNAL(click()), w, SLOT(clicked()));
  13.     }
  14.  
  15. }

August 14, 2011

mlong mlong
Robot Herder
1537 posts

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

  1. login = new loginBox(this);

add
  1. 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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 14, 2011

kalster kalster
Lab Rat
233 posts

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

August 14, 2011

mlong mlong
Robot Herder
1537 posts

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 14, 2011

kalster kalster
Lab Rat
233 posts

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

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "loginbox.h"
  4. #include <QRegExp>
  5. #include <QSettings>
  6. //#include <QTcpSocket>
  7.  
  8. QString serverLineEdit;
  9. QString userLineEdit;
  10. int connect1 = 0;
  11.  
  12.  
  13. MainWindow::MainWindow(QWidget *parent) :
  14.     QMainWindow(parent),
  15.     ui(new Ui::MainWindow)
  16. {
  17.     ui->setupUi(this);
  18.     login = new loginBox(this);
  19.  
  20.     login->setModal(true);
  21.    login->show();
  22. connect(login, SIGNAL(click()), this, SLOT(clicked()));
  23.  
  24. }
  25.  
  26. void MainWindow::clicked(){
  27.     QSettings settings("config.ini", QSettings::IniFormat);
  28.     serverLineEdit = settings.value("serverLineEdit").toString();
  29.     userLineEdit = settings.value("userLineEdit").toString();
  30.     connect1 = settings.value("ConnectToServer").toInt();
  31.         socket = new QTcpSocket(this);
  32.  
  33.         this->connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
  34.         this->connect(socket, SIGNAL(connected()), this, SLOT(connected()));
  35.  
  36.         socket->connectToHost(serverLineEdit, 4200);
  37. }

and the dialog.cpp

  1. #include "loginbox.h"
  2. #include "ui_loginbox.h"
  3. #include "mainwindow.h"
  4. #include <QRegExp>
  5. #include <QSettings>
  6. int connect2;
  7.  
  8. loginBox::loginBox(QWidget *parent) :
  9.     QDialog(parent),
  10.     ui(new Ui::loginBox)
  11. {
  12.     ui->setupUi(this);
  13. }
  14.  
  15. loginBox::~loginBox()
  16. {
  17.     delete ui;
  18. }
  19.  
  20. void loginBox::on_loginButton_clicked()
  21. {
  22.     connect2 = 1;
  23.     //save the result of serverLineEdit to the config.ini file
  24.     QSettings settings("config.ini", QSettings::IniFormat);
  25.     settings.setValue("serverLineEdit", ui->serverLineEdit->text());
  26.     settings.setValue("userLineEdit", ui->userLineEdit->text());
  27.     settings.setValue("ConnectToServer", connect2);
  28.     this->close();
  29.  
  30. }

August 14, 2011

mlong mlong
Robot Herder
1537 posts

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 14, 2011

kalster kalster
Lab Rat
233 posts

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

August 14, 2011

mlong mlong
Robot Herder
1537 posts

  1. emit click();

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

Page  
1

  ‹‹ [Moved] control master volume from Qt creator application      [Solved]Wrong QRegExp ››

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