Results 1 to 10 of 10

Thread: accessing data from mainwindow to change second window

  1. #1
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default accessing data from mainwindow to change second window

    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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing data from mainwindow to change second window

    Qt Code:
    1. connect(QlineEdit_1,SIGNAL(textChanged(const QString &),QlineEdit_2,SLOT(setText(const QString &)));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: accessing data from mainwindow to change second window

    Quote Originally Posted by Lesiok View Post
    Qt Code:
    1. connect(QlineEdit_1,SIGNAL(textChanged(const QString &),QlineEdit_2,SLOT(setText(const QString &)));
    To copy to clipboard, switch view to plain text mode 
    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
    #
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8. private slots:
    9.  
    10. void on_pushButton_next_pressed();
    11.  
    12. signals:
    13. void on_nodes1_textchanged(const QString &);
    To copy to clipboard, switch view to plain text mode 
    #

    secDialog.h
    #
    class secDialog;
    }

    Qt Code:
    1. class secDialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit secDialog(QWidget *parent = 0);
    7. ~secDialog();
    8. public slots:
    9. void on_carry_nodes_setText(const QString &);
    10. }
    To copy to clipboard, switch view to plain text mode 
    #

    mainwindow.cpp
    #
    Qt Code:
    1. MainWindow::~MainWindow()
    2. {
    3. delete ui;
    4. connect (on_nodes1_textchanged,(SIGNAL(textChanged(const QString &)),
    5. on_carry_nodes_setText,SLOT(setText(const QString &)))); // here where i get an error as on_carry_nodes_setText
    6. //was not declared on this scope
    7. void MainWindow::on_pushButton_next_pressed()
    8. {
    9. secDialog secdialog;
    10. secdialog.setModal(true);
    11. secdialog.exec();
    12. emit this->on_nodes1_textchanged(ui->nodes1->text());
    13. }
    To copy to clipboard, switch view to plain text mode 
    #
    Last edited by high_flyer; 25th April 2017 at 17:10.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: accessing data from mainwindow to change second window

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: accessing data from mainwindow to change second window

    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 &))));
    ^

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing data from mainwindow to change second window

    Read about QObject::connect.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: accessing data from mainwindow to change second window

    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:

    Qt Code:
    1. connect ( this, SIGNAL(on_nodes1_textchanged(const QString &)), ???, SLOT(on_carry_nodes_setText(const QString &)));
    To copy to clipboard, switch view to plain text mode 

    "???" 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:

    Qt Code:
    1. void MainWindow::on_pushButton_next_pressed()
    2. {
    3. secDialog secdialog;
    4. // secdialog.setModal(true); -- not needed. the call to exec() makes secdialog modal.
    5. secdialog.on_carry_nodes_setText( ui->nodes1->text() );
    6. secdialog.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    zakachoka (2nd May 2017)

  9. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: accessing data from mainwindow to change second window

    or in short, RTFM.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #9
    Join Date
    Apr 2017
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: accessing data from mainwindow to change second window

    Quote Originally Posted by d_stranz View Post
    "???" 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?
    Qt Code:
    1. secDialog *secdialog;
    To copy to clipboard, switch view to plain text mode 
    would secdialog be a pointer for that window or would it be an object am confused

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: accessing data from mainwindow to change second window

    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:

    Qt Code:
    1. int myInt;
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. int main( int argc, char * argv[] )
    2. {
    3. QApplication a( argc, argv );
    4. MainWindow w;
    5. w.show();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Accessing data of .qml file
    By jeff28 in forum Newbie
    Replies: 1
    Last Post: 29th August 2012, 15:15
  2. QTableView : accessing the data
    By marvaneke in forum Newbie
    Replies: 10
    Last Post: 30th March 2012, 11:31
  3. Mainwindow layout change
    By Anshuman in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2011, 01:33
  4. accessing my main application window widget
    By jayw710 in forum Newbie
    Replies: 8
    Last Post: 15th November 2007, 19:33
  5. accessing data in a QFile
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 16:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.