View Full Version : How to Output Strings in MainWindow which are generated in external .cpp file
Lumbricus
12th November 2015, 18:20
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:
ui->textBrowser->setText(str);
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!
anda_skoa
12th November 2015, 19:18
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,
_
Lumbricus
12th November 2015, 19:53
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
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_lineEdit_metakernel_editingFinished();
void on_pushButton_4_clicked();
void on_lineEdit_editingFinished();
void print_to_browser(QString str);
private:
Ui::MainWindow *ui;
};
Function in mainwindow.ccp
void MainWindow::print_to_browser(QString str)
{
ui->textBrowser->setText(str);
}
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?
anda_skoa
13th November 2015, 08:50
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,
_
Lumbricus
14th November 2015, 10:20
Thx, passing the QTextBrowser pointer to the function directly let me modify it in the function.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.