Solved Qpaint,Qtimer,Qrect and Signal and Slots
-
hello and happy new year.
I'm having a problem when painting a rectangle, I have a function that I call Debug that I use as a Slot in a connection with Qtimer, that when a second passes it should change the size of the rectangle and move to the next position.
In that function I created a list with the children of my menu bar and an empty list where I am introducing them, (I create the empty list for problems with the index). Once the empty list has been created in a for loop, I go through that list and through the acctiongeometry function I assign the size to the recatangle that changes as I go through the list. Here you can see the function.///DEBUG FUNCTION///
void MainWindow::Debug(){
QList<QMenu*> Menus = ui->menubar->findChildren<QMenu*>(); QList<QMenu*> aux; for(int l=0;l<Menus.length()-1;l++){ aux.insert(l,Menus.at(l)); qDebug()<<"aux"<<menuBar()->actionGeometry(aux.at(l)->menuAction()); } for(int y=0;y<aux.length();y++){ mrect.moveTo(menuBar()->actionGeometry(aux.at(y)->menuAction()).x(),menuBar()->actionGeometry(aux.at(y)->menuAction()).y()); mrect = QRect(menuBar()->actionGeometry(aux.at(y)->menuAction())); qDebug()<<mrect; }
}
My painter is the following one where I basically pass the rectangle and the color of the brush:
void MainWindow::paintEvent(QPaintEvent *event){
Q_UNUSED(event)
QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawRect(mrect);}
The constructors of the rectangle:QRect MainWindow::nRect() const{
return mrect;
}
void MainWindow::setNRect(const QRect &rect){
mrect = rect;
}
And finally the timer where with whom I make the connections, I have two one with the Debug slot and another to do an update every time a second passes.connect(&timer,SIGNAL(timeout()), this, SLOT(Debug())); connect(&timer, SIGNAL(timeout()), this, SLOT(update())); timer.start(1000);
variables and functions that I have in my header file:
Q_PROPERTY(QRect nrect READ nRect WRITE setNRect)
void Debug();
void paintEvent(QPaintEvent *event);QRect mrect1;
QTimer timer;My problem is that when I run the program the rectangle appears in the last position of the empty list and what I want is to see how it goes from the initial position to the end, not just the final one.
I have been looking to see if the problem is in the loop or when filling the list but with when I print on the screen there are neither of the two problems. There may be a problem with the Qpainter but I don't know what it is or the connections but I don't know what it can be either.qDebug()<<"aux"<<menuBar()->actionGeometry(aux.at(l)->menuAction());
The output:
aux QRect(2,2 93x34)
aux QRect(95,2 89x34)
aux QRect(184,2 47x34)
aux QRect(231,2 159x34)
aux QRect(390,2 81x34)qDebug()<<mrect;
The output:
QRect(2,2 93x34)
QRect(95,2 89x34)
QRect(184,2 47x34)
QRect(231,2 159x34)
QRect(390,2 81x34)Please help me because I am very lost.
thanks.
-
Hi,
Your loop always updates your mrect variable with all the values of your list. There mrect will end with the last value of the list. If you mrect to pass by each value, you need to use an index representing the current one you want to show and use that to update mrect. That index should be incrémenter once per timeout. Do not forget to put it back to zero when needed.
-
Hi,
Your loop always updates your mrect variable with all the values of your list. There mrect will end with the last value of the list. If you mrect to pass by each value, you need to use an index representing the current one you want to show and use that to update mrect. That index should be incrémenter once per timeout. Do not forget to put it back to zero when needed.
-
This post is deleted!