PDA

View Full Version : accessing data from mainwindow to change second window



zakachoka
25th April 2017, 08:24
Hi, any help would be much appreciated :)

I have a mainwindow with QlineEdit_1,
and a second window with another QlineEdit_2, i have made a button to open the second window from mainwindow

my question is how to make QlineEdit_2 show a certain text when the value "1" is entered in QlineEdit_1

thank you

Lesiok
25th April 2017, 10:09
connect(QlineEdit_1,SIGNAL(textChanged(const QString &),QlineEdit_2,SLOT(setText(const QString &)));

zakachoka
25th April 2017, 16:02
connect(QlineEdit_1,SIGNAL(textChanged(const QString &),QlineEdit_2,SLOT(setText(const QString &)));

Lesiok thank you for replying , please bear with me as am a newbie in Qt and have read many tutorials but still :(

can u please check my coding as it gives an error, i have a mainwindow with push button (to go to the secdialog window) and QlineEdit named nodes1. A secDialog with QlineEdit named carry_nodes

mainwindow.h
#

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:

void on_pushButton_next_pressed();

signals:
void on_nodes1_textchanged(const QString &);

#

secDialog.h
#
class secDialog;
}


class secDialog : public QDialog
{
Q_OBJECT

public:
explicit secDialog(QWidget *parent = 0);
~secDialog();
public slots:
void on_carry_nodes_setText(const QString &);
}
#

mainwindow.cpp
#

MainWindow::~MainWindow()
{
delete ui;
connect (on_nodes1_textchanged,(SIGNAL(textChanged(const QString &)),
on_carry_nodes_setText,SLOT(setText(const QString &)))); // here where i get an error as on_carry_nodes_setText
//was not declared on this scope
void MainWindow::on_pushButton_next_pressed()
{
secDialog secdialog;
secdialog.setModal(true);
secdialog.exec();
emit this->on_nodes1_textchanged(ui->nodes1->text());
}
#

high_flyer
25th April 2017, 17:12
can u please check my coding as it gives an error,
It would help if you state what the error is, instead of let us play the role of a compiler.

zakachoka
26th April 2017, 03:26
hi,
I get an error in mainwindow.cpp as: error: 'on_carry_nodes_setText' was not declared in this scope
on_carry_nodes_setText,SLOT(setText(const QString &))));
^

Lesiok
26th April 2017, 06:36
Read about QObject::connect.

d_stranz
27th April 2017, 01:12
connect (on_nodes1_textchanged,(SIGNAL(textChanged(const QString &)),
on_carry_nodes_setText,SLOT(setText(const QString &))));

First, why are you calling connect() in the MainWindow destructor? The MainWindow instance is being destroyed - nothing you do here will have any effect. The connect() call should be in the constructor if anywhere.

Second, the syntax of your call is not correct. You do not put the names of the signal or slot functions, you put the names of the variables for the instances of the classes that implement those functions. So in your case, this should be:



connect ( this, SIGNAL(on_nodes1_textchanged(const QString &)), ???, SLOT(on_carry_nodes_setText(const QString &)));


"???" means I don't know what you have named the variable that points to your secDialog instance, so you should put that name there. Of course, if that variable is not in scope at the place you write that connect() call, then it still won't compile. Since the only place in the code you have posted that declares a variable of this class is in the push button pressed slot, that's where this connect() statement should go, with "???" replaced by "&secdialog" (without quotes, of course).

But you actually don't need to do this at all. A slot is just a plain old C++ class method, so you can just call it directly:



void MainWindow::on_pushButton_next_pressed()
{
secDialog secdialog;
// secdialog.setModal(true); -- not needed. the call to exec() makes secdialog modal.
secdialog.on_carry_nodes_setText( ui->nodes1->text() );
secdialog.exec();
}

Finally, if you want standard UI behavior, you should be handling the QPushButton::clicked() signal, not QPushButton::pressed(). Your code will post the dialog as soon as the mouse button goes down on the UI button. Normal behavior is to handle the sequence mouse down mouse up, where both events occur on the UI button. That's what clicked() does. In normal GUI behavior, pressing the mouse button while on a GUI button, then moving off the GUI button and releasing the mouse will not count as a click.

high_flyer
27th April 2017, 10:10
or in short, RTFM.

zakachoka
2nd May 2017, 05:44
"???" means I don't know what you have named the variable that points to your secDialog instance.

thank you for all the corrections u mentioned, i have a really dumb question regarding the secDialog point, isnt that written in this way?

secDialog *secdialog;
would secdialog be a pointer for that window or would it be an object am confused

d_stranz
2nd May 2017, 23:38
would secdialog be a pointer for that window or would it be an object am confused

In the code I posted, I am creating a temporary instance of secDialog (named "secdialog") on the stack. This is no different that if you had declared an "int" variable the same way:



int myInt;


In both cases, the instance this creates only lives (is in scope) for the duration of the function and it is automatically destroyed when the function exits. This is basic C++, and you should read about "variables and scoping" in your C++ book.

I declared "secdialog" in this way because it is a common coding convention in Qt - if you have a modal dialog that is created, posted, and then goes away when the user clicks the "Ok" button, then it is common to create it on the stack. Everything is cleaned up when the button click slot exits.

In the main() function in your main.cpp file, you are probably doing something very similar with the instances of QApplication and MainWindow:



int main( int argc, char * argv[] )
{
QApplication a( argc, argv );
MainWindow w;
w.show();
return a.exec();
}