Results 1 to 4 of 4

Thread: How to update the components or repaint of a QMainWindow?

  1. #1
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default How to update the components or repaint of a QMainWindow?

    Hi I'm making simple things on a QMainWindow to see how it works, but I once I click a button to do something, nothing seems to change in the main window.
    I tried to use repaint() and update() funtions but nothing happens.

    And by the way, can someone explain me the difference between repaint() and update()? I understand what means each word, but if I'll use repaint(), the window gonna show the changes too? or I have to use the update() function?

    All right to the point, My Code:
    Qt Code:
    1. void Window::labels(){
    2.  
    3. example = new QLabel();
    4. example->setText("Ejemplo: ");
    5. acep = new QPushButton();
    6. acep->setText("Aceptar");
    7. linea = new QLineEdit();
    8. linea->resize(10,30);
    9. connect(acep,SIGNAL(clicked()),linea,SLOT(modify()));
    10.  
    11. layoutH = new QHBoxLayout();
    12. layoutH->addSpacing(20);
    13. layoutH->addWidget(example);
    14. layoutH->addSpacing(10);
    15. layoutH->addWidget(acep);
    16. layoutH->addSpacing(20);
    17. layoutH->addWidget(linea);
    18. layoutH->addSpacing(20);
    19. container = new QWidget();
    20. container->setLayout(layoutH);
    21. this->setCentralWidget(container);
    22. }
    23. void Window::modify(){
    24. line = linea->text(); //QString line, QEditLine linea, I know have closely the same name, but Im just testing simple things to see how it works.
    25. line.append('s'); //
    26. linea->setText(line);
    27. linea->repaint(); // I used update too, and with the Window too, but nothing happens
    28. this->update();
    29. }
    To copy to clipboard, switch view to plain text mode 

    I tried to append a s to the QLineEdit to see if changes it, tried to clear the text too, but nothing happens.
    I tried to set the window title with the text given in the QLineEdit, but not working too.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to update the components or repaint of a QMainWindow?

    You do not need to call either repaint() or update() in your example. Calling either function is unusual in a generic Qt program.

    The problem is that modify() is never called. You have connected the clicked() signal to the line edit's modify() slot... except it does not have one. The modify() function (is it declared a slot?) is a member of the Window class not the QLineEdit class. You should be seeing a warning message in the application output at run time because the connection cannot be made.
    Qt Code:
    1. connect(acep, SIGNAL(clicked()), this, SLOT(modify()));
    2. // or
    3. connect(acep, SIGNAL(clicked()), SLOT(modify()));
    To copy to clipboard, switch view to plain text mode 
    The repaint() call immediately redraws the widget it is called on. The update() call schedules a repaint() to occur some (short) time in the future when the program next returns to the event loop. Multiple calls to update() are generally merged into a single repaint of the widget at the appropriate time.

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

    vitaR (18th March 2014)

  4. #3
    Join Date
    Mar 2014
    Posts
    27
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update the components or repaint of a QMainWindow?

    Quote Originally Posted by ChrisW67 View Post
    You do not need to call eit...
    Still not working, but I understand what you said, thanks now I got some things clear, so.. I have to set modify function as a SLOT to make this work?
    Last edited by vitaR; 18th March 2014 at 23:59.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to update the components or repaint of a QMainWindow?

    Yes, in the header for Window:
    Qt Code:
    1. class Window: public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. Window(...);
    6.  
    7. ...
    8. private slots: //
    9. void modify();
    10. ...
    11. };
    To copy to clipboard, switch view to plain text mode 
    and you must rerun qmake if you added the Q_OBJECT macro.

  6. The following user says thank you to ChrisW67 for this useful post:

    vitaR (19th March 2014)

Similar Threads

  1. repaint/update stops updating GUI
    By gokceng in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th November 2011, 15:35
  2. Replies: 4
    Last Post: 17th October 2010, 23:30
  3. Replies: 4
    Last Post: 10th June 2010, 22:25
  4. Replies: 5
    Last Post: 18th April 2007, 11:29
  5. Use of repaint/update
    By Placido Currò in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2007, 20:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.