[SOLVED] calling a function outside of the dialog class
-
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
-
Not a working example, but a way to go
@
class MainWindow : public QMainWindow
{
//more suff
public slots:
void clicked();
}
class Dialog : public QDialog
{public:
Dialog(MainWindow *w)
{
connect(this,SIGNAL(click()),w,SLOT(clicked()));
}
signals:
void click();
}
@ -
i got the code working put now all my dialog widgets have disappeared.
-
How did you integrate the concepts in the code above into your own code?
-
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_OBJECTpublic:
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
@ -
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. :-)
-
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();
@ -
sure, i will take your advice on constructors mlong.
-
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())); }
}@
-
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.
-
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
-
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.
-
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();}@
-
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.
-
can i have an example on how to emit the click() signal in the dialog.cpp file?
-
@
emit click();
@ -
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. :)
-
thanks a bunch mlong. the code works great now :)
yes, i will read signals and slots -
Glad it's working!
-
also, i am starting to understand what can and can't be done inside of a constructor.