[SOLVED] help getting signal and slot to work
-
I have signal and slot in my dialog.cpp file. the readyRead function that it refers to is at the mainwindow.cpp. how can i call the signal and slot function from outside of the class? Can i have an example please. thank you in advanced.
also, i have socket defined in two places. at the mainwindow and dialog.cpp
@socket = new QTcpSocket(this);@
i think that it should only be defined at one place. how to tell qt to use socket from dialog for mainwindow? socket needs to be read at the mainwindow and dialog. how to only Instantiate it once while the second time it reads and writes to the server.dialog.cpp
@connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));@mainwindow.cpp
@
void MainWindow::readyRead(){
}@ -
Hi kalster,
that sounds a bit of a weird design.
You want to have a main window which holds a dilog. The dialog has a socket as member. When the socket does something, the main widnow should access it and handle the socket?The connect si not the problem, if you have the pointer to the main window. Or you connect the sockets signal with a signal of the dialog (signal forwarding) and connect the dialogs signal with a slot in the main window.
But I would not let the main window access a member of the dialog directly.
-
the lineEdit in the dialog stores the username, i am having problems getting that username to the mainwindow. when the user clicks the ok button on the dialog then the mainwindow does all the sockets, but the time the dialog is opened, the mainwindow does the sockets stuff anyways which is too early to do and the result is i cant connect anymore. I basiclly need to delay the mainwindow from doing sockets until the user clicks the ok button on the dialog but i don't know how
because i dont know how to get a variable from the dialog to the mainwindow, at the dialog open the socket then try to connect to the server. from there, the dialog is closed and the mainwindow does the rest. the problem is that the mainwindow does the rest before the dialog is closed.
-
So the problem is not to store a value for a later run with QSettings, your problem is to get a value that was entered in a dialog from the caller.
That's pretty easy. Give your dialog a function gettext() and return the value of the edit field.
Or store the value of the edit field in a variable in the virtual method QDIalog::accept() -
You could have something like that:
@class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = 0);const QString login() const; const QString password() const;
private:
class QLineEdit *loginEdit, *passwordEdit;
};LoginDialog::LoginDialog(QWidget *parent)
: QDialog(parent)
, loginEdit(new QLineEdit(this))
, passwordEdit(new QLineEdit(this))
{
passwordEdit->setEchoMode(QLineEdit::Password);
QFormLayout * form = new QFormLayout(this);
form->addRow("Login", loginEdit);
form->addRow("Password", passwordEdit);QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); form->addRow(buttonBox); connect(buttonBox, SIGNAL(accepted()), SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
}
const QString LoginDialog::login() const {
return loginEdit->text();
}const QString LoginDialog::password() const {
return passwordEdit->text();
}
@
And you would use the class in your mainwindow function like that:
@void MainWindow::askLoginInformation() {
LoginDialog dialog(this);
// Show the dialog and wait until the user clicks Ok/Cancel or closes it.
if(dialog.exec() == QDialog::Accepted) {
// The dialog was validated by the user
QString login = dialog.login();
QString password = dialog.password();// Do something with login and password } else { // The dialog was closed or the cancel button was clicked }
}@