PDA

View Full Version : QT QTextBrowser problem with setText



fäbs
5th May 2015, 11:58
HI,
i have a problem with my qt program.
first of all i have a QTextBrowser and a Button. when the button is clicked it should call a method from a c++ class like this:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_Button_clicked()
{
try{
GUIController guiController;
guiController.somemethode();

}
catch (char const* err){
cout << err << endl;

}

}

This called methode should do something and then call a method from mainwindow class which should set Text to the textBrowser like this:



GUIController::GUIController()
{

}

GUIController::~GUIController()
{

}

void GUIController::somemethode{
...
...
mainwindow.printToOutput(("text"));

}




MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::printToOutput(QString text){

ui->outputText->append(text);

}


But the problem is that the text doesn't appear in the QTextBrowser.
Has anybody an idea whats the problem?
i googled a long time and i think the problem is something with the "render loop" of the gui but i don't know how to fix it.

Thanks for help!

anda_skoa
5th May 2015, 13:06
Your GUIController instance does not have any access to the MainWindow instance.
How do you expect the controller to affect an object it doesn't know about?

Cheers,
_

fäbs
5th May 2015, 13:44
sry i didn't post the header file of my guicontroller.



class GUIController
{
public:
GUIController();
~GUIController();
somemethod();

private:
MainWindow mainwindow;

};


so my guicontroller has a instance of mainwindow so i think its not an access problem... :(

i also started another test and changed my printtoOutput method like this:


void MainWindow::printToOutput(QString text){
ui->outputText->setText("sdf");
QMessageBox::information(this,"sd","test");
}


setText to my QTextBrowser has no effect, but QMessageBox appear.

Added after 9 minutes:

i also tried to set a text to my QTextBrowser and read the text from the QTextBrowser and push it to a QMassageBox.



void MainWindow::printToOutput(QString text){
ui->outputText->setText("sdf");
QMessageBox::information(this,"sd",(ui->outputText->toPlainText()));
}

so in the MesageBox the text i set a line before ("sdf") appear, but not in the QTextBrowser as it should....

anda_skoa
5th May 2015, 15:03
sry i didn't post the header file of my guicontroller.



class GUIController
{
public:
GUIController();
~GUIController();
somemethod();

private:
MainWindow mainwindow;

};


so my guicontroller has a instance of mainwindow so i think its not an access problem... :(

And you are sure you want to access this second main window and not the main window that created the GUIController?

Cheers,
_

fäbs
5th May 2015, 15:52
OH NO :D
i solved the problem some minutes ago, thank you very much for your tipp!!!
i spent so much time searching the problem on the wrong place :(

of course the problem was accessing mainwindow.

now i changed my instance to:


MainWindow * mainwindow = (MainWindow *) qApp->activeWindow();

and everything work :)

thanks for help!!

anda_skoa
5th May 2015, 18:35
OH NO :D

Thought so :)



now i changed my instance to:


MainWindow * mainwindow = (MainWindow *) qApp->activeWindow();

and everything work :)

You could also pass "this" to the constructor of GUIController or to the method you are calling.

Cheers,
_