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:
Code:
void Window::labels(){
example->setText("Ejemplo: ");
acep->setText("Aceptar");
linea->resize(10,30);
connect(acep,SIGNAL(clicked()),linea,SLOT(modify()));
layoutH->addSpacing(20);
layoutH->addWidget(example);
layoutH->addSpacing(10);
layoutH->addWidget(acep);
layoutH->addSpacing(20);
layoutH->addWidget(linea);
layoutH->addSpacing(20);
container->setLayout(layoutH);
this->setCentralWidget(container);
}
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->setText(line);
linea->repaint(); // I used update too, and with the Window too, but nothing happens
this->update();
}
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.
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.
Code:
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.
Re: How to update the components or repaint of a QMainWindow?
Quote:
Originally Posted by
ChrisW67
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?
Re: How to update the components or repaint of a QMainWindow?
Yes, in the header for Window:
Code:
{
Q_OBJECT
public:
Window(...);
...
private slots: //
void modify();
...
};
and you must rerun qmake if you added the Q_OBJECT macro.