View Full Version : How to update the components or repaint of a QMainWindow?

18th March 2014, 21:41
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:

void Window::labels(){

example = new QLabel();
example->setText("Ejemplo: ");
acep = new QPushButton();
linea = new QLineEdit();
connect(acep,SIGNAL(clicked()),linea,SLOT(modify() ));

layoutH = new QHBoxLayout();
container = new QWidget();
void Window::modify(){
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.
line.append('s'); //
linea->repaint(); // I used update too, and with the Window too, but nothing happens

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.

18th March 2014, 22:02
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.

connect(acep, SIGNAL(clicked()), this, SLOT(modify()));
// or
connect(acep, SIGNAL(clicked()), SLOT(modify()));

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.

18th March 2014, 22:53
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?

18th March 2014, 23:47
Yes, in the header for Window:

class Window: public QMainWindow

private slots: //
void modify();

and you must rerun qmake if you added the Q_OBJECT macro.