Results 1 to 6 of 6

Thread: QT QTextBrowser problem with setText

  1. #1

    Default QT QTextBrowser problem with setText

    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:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. }
    8.  
    9. MainWindow::~MainWindow()
    10. {
    11. delete ui;
    12. }
    13.  
    14. void MainWindow::on_Button_clicked()
    15. {
    16. try{
    17. GUIController guiController;
    18. guiController.somemethode();
    19.  
    20. }
    21. catch (char const* err){
    22. cout << err << endl;
    23.  
    24. }
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. GUIController::GUIController()
    2. {
    3.  
    4. }
    5.  
    6. GUIController::~GUIController()
    7. {
    8.  
    9. }
    10.  
    11. void GUIController::somemethode{
    12. ...
    13. ...
    14. mainwindow.printToOutput(("text"));
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. }
    8.  
    9. MainWindow::~MainWindow()
    10. {
    11. delete ui;
    12. }
    13.  
    14. void MainWindow::printToOutput(QString text){
    15.  
    16. ui->outputText->append(text);
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    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!

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

    Default Re: QT QTextBrowser problem with setText

    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,
    _

  3. #3

    Default Re: QT QTextBrowser problem with setText

    sry i didn't post the header file of my guicontroller.

    Qt Code:
    1. class GUIController
    2. {
    3. public:
    4. GUIController();
    5. ~GUIController();
    6. somemethod();
    7.  
    8. private:
    9. MainWindow mainwindow;
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. void MainWindow::printToOutput(QString text){
    2. ui->outputText->setText("sdf");
    3. QMessageBox::information(this,"sd","test");
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. void MainWindow::printToOutput(QString text){
    2. ui->outputText->setText("sdf");
    3. QMessageBox::information(this,"sd",(ui->outputText->toPlainText()));
    4. }
    To copy to clipboard, switch view to plain text mode 
    so in the MesageBox the text i set a line before ("sdf") appear, but not in the QTextBrowser as it should....
    Last edited by fäbs; 5th May 2015 at 13:44.

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

    Default Re: QT QTextBrowser problem with setText

    Quote Originally Posted by fäbs View Post
    sry i didn't post the header file of my guicontroller.

    Qt Code:
    1. class GUIController
    2. {
    3. public:
    4. GUIController();
    5. ~GUIController();
    6. somemethod();
    7.  
    8. private:
    9. MainWindow mainwindow;
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    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,
    _

  5. #5

    Default Re: QT QTextBrowser problem with setText

    OH NO
    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:
    Qt Code:
    1. MainWindow * mainwindow = (MainWindow *) qApp->activeWindow();
    To copy to clipboard, switch view to plain text mode 
    and everything work

    thanks for help!!

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

    Default Re: QT QTextBrowser problem with setText

    Quote Originally Posted by fäbs View Post
    OH NO
    Thought so

    Quote Originally Posted by fäbs View Post
    now i changed my instance to:
    Qt Code:
    1. MainWindow * mainwindow = (MainWindow *) qApp->activeWindow();
    To copy to clipboard, switch view to plain text mode 
    and everything work
    You could also pass "this" to the constructor of GUIController or to the method you are calling.

    Cheers,
    _

Similar Threads

  1. QPushButton setText problem
    By batileon in forum Qt Programming
    Replies: 3
    Last Post: 3rd December 2008, 03:02
  2. QLabel setText Problem
    By pmabie in forum Qt Programming
    Replies: 10
    Last Post: 1st November 2007, 23:32
  3. Problem with QLabel and setText
    By jambrek in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2007, 16:02
  4. Problem with QLineEdit and setText
    By Elmo23x in forum Qt Programming
    Replies: 8
    Last Post: 12th April 2007, 12:35
  5. Qtable--setText problem
    By :db:sStrong in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2006, 10: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.