Results 1 to 5 of 5

Thread: How to Output Strings in MainWindow which are generated in external .cpp file

  1. #1
    Join Date
    Oct 2015
    Posts
    33
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    14

    Question How to Output Strings in MainWindow which are generated in external .cpp file

    Hi Friends,

    I have a function that does some calculations(find_other_comets.cpp) and want to update a QTextBrowser widget (in mainwindow.cpp) with text generated in that function. I press a button and my calculation is called, at some times i want to output the progress/data to the Textbrowser. If i understand right i cannot acces the main window from the outside ( find_other_comets.cpp)?! I tried using this in the external cpp:
    Qt Code:
    1. ui->textBrowser->setText(str);
    To copy to clipboard, switch view to plain text mode 

    My question is what is the right approch to pass Strings to the TextBrowser in mainwindow? if additional code or information is needed im happy to supply it!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: How to Output Strings in MainWindow which are generated in external .cpp file

    There are a couple of options, like having access to the textBrowser for the code in that other file.

    But probably the easiest way is to make the other code emit a signal with the text and the connect that signal to the setText() slot.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    33
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: How to Output Strings in MainWindow which are generated in external .cpp file

    thx for taking your time!

    I have tried using signals and slots but got confused while trying. How exactly does one emit a signal when i haven't created an object in the external cpp file? I understand that i have to create a slot in mainwindow and a function that will be run when the slot catches a signal.

    header mainwindow.h

    Qt Code:
    1. namespace Ui {
    2. class MainWindow;
    3. }
    4.  
    5. class MainWindow : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12.  
    13. private slots:
    14. void on_pushButton_clicked();
    15.  
    16. void on_lineEdit_metakernel_editingFinished();
    17.  
    18. void on_pushButton_4_clicked();
    19.  
    20. void on_lineEdit_editingFinished();
    21.  
    22. void print_to_browser(QString str);
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    To copy to clipboard, switch view to plain text mode 

    Function in mainwindow.ccp

    Qt Code:
    1. void MainWindow::print_to_browser(QString str)
    2. {
    3. ui->textBrowser->setText(str);
    4. }
    To copy to clipboard, switch view to plain text mode 

    But i'm kinda lost on how to emit the signal or even define it if didnt creat an objectin in find_other_comets.ccp. And following that the connect statement is not clear also. Can any one give me a example?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: How to Output Strings in MainWindow which are generated in external .cpp file

    Well, emitting a signal does indeed require an object.
    Since you gave no indication what your find_other_comets.cpp looks like I assume that you had some object in there.

    If it contains just a function, then pass the pointer of the text browser to the function and call it directly.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Lumbricus (14th November 2015)

  6. #5
    Join Date
    Oct 2015
    Posts
    33
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: How to Output Strings in MainWindow which are generated in external .cpp file

    Thx, passing the QTextBrowser pointer to the function directly let me modify it in the function.

Similar Threads

  1. Replies: 1
    Last Post: 5th March 2014, 00:35
  2. Replies: 2
    Last Post: 11th February 2013, 22:28
  3. Qt input output / external programs
    By konnosm in forum Newbie
    Replies: 1
    Last Post: 15th July 2012, 11:21
  4. Qt Creator Can I post links to code lines in output pane using external tools?
    By mrsomeonee in forum Qt Tools
    Replies: 0
    Last Post: 25th August 2011, 13:15
  5. Redirect standard output of external tools
    By nina1983 in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2011, 10:09

Tags for this Thread

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.