Results 1 to 3 of 3

Thread: Connect one form to another

  1. #1
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Connect one form to another

    I am trying to connect a QFontCombo box from font_select to a QTextBox in mainwindow. I am new to Qt and have no idea how to use the connect() function to do so as i want user to select font in the new form and hit an OK buton and use that font in the text box on the previous form. Can anone please help???

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connect one form to another

    If all you want the user to do is select a new font, then using a QFontDialog would be easier. If your form is more complex than that (with more than just font properties on it), then you could do something like this:

    1 - Create a signal on the form called fontChanged() (or something like that). In the form constructor, connect the QFontComboBox::currentFontChanged() signal to this signal.

    Qt Code:
    1. // MyCostomForm.h
    2. class MyCustomForm : public QWidget
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. MyCustomForm( QWidget * parent = 0 );
    8. ~MyCustomForm();
    9.  
    10. signals:
    11. void fontChanged( const QFont & font );
    12.  
    13. // ...
    14.  
    15. private:
    16. Ui::MyCustomForm * ui;
    17. };
    18.  
    19. // MyCustomForm.cpp
    20. MyCustomForm::MyCustomForm( QWidget * parent ) : QWidget( parent ), ui( new Ui::MyCustomForm )
    21. {
    22. ui->setupUi( this );
    23.  
    24. // It's OK to connect signal to signal
    25. connect( ui->font_select, &QFontComboBox::currentFontChanged, this, &MyCustomForm::fontChanged );
    26. }
    To copy to clipboard, switch view to plain text mode 

    2 - Create a slot on MainWindow called onFontChanged() (or something like that). When you create the instance of MyCustomForm, connect this slot to the form's signal:

    Qt Code:
    1. // MainWindow.h
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. // ...
    6.  
    7. private slots:
    8. void onFontChanged( const QFont & font );
    9.  
    10. private:
    11. MyCustomForm * myForm;
    12.  
    13. };
    14.  
    15. // MainWindow.cpp
    16.  
    17. MainWindow::MainWindow( QWidget * parent ) : QMainWindow( parent )
    18. {
    19. myForm = new MyCustomForm( this );
    20.  
    21. // Here we connect signal to slot
    22. connect ( myForm, &MyCustomForm::fontChanged, this, &MainWindow::onFontChanged );
    23.  
    24. // ...
    25. }
    To copy to clipboard, switch view to plain text mode 

    3 - In the slot, do what you need to change the font in the QTextEdit:

    Qt Code:
    1. void MainWindow::onFontChanged( const QFont & newFont )
    2. {
    3. // Change the font in the QTextEdit
    4. }
    To copy to clipboard, switch view to plain text mode 

    This is the general model used to transfer information between two GUI windows in Qt. You should never expose the actual QWidget variables in the GUI windows by making them public. Instead, handle their signals locally within each GUI window, and if the change needs to be sent to other windows, implement signals to transmit them. In the receiving window, implement a slot that connects to the signal from the other window.

    With this model, you can completely replace the second window with a different implementation without any effect on the receiving window, so long as the signals and slots remain the same.
    <=== 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.

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

    bakslash (18th November 2016)

  4. #3
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connect one form to another

    Thank you so much!!! Everything makes so much more sense now

Similar Threads

  1. Replies: 3
    Last Post: 18th July 2013, 04:12
  2. Replies: 7
    Last Post: 23rd May 2012, 12:00
  3. Replies: 5
    Last Post: 17th June 2011, 09:43
  4. How to hide from taskbar a form showed form mainwindow
    By tonnot in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 14:36
  5. Replies: 5
    Last Post: 12th March 2010, 21:43

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
  •  
Qt is a trademark of The Qt Company.