why my dynamic label text get overlap ? what i need to do avoid this?
-
#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <qdatetime.h>
#include <qtimer.h>
#include <qthread.h>
#define TIMEOUT 300Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);QTimer::singleShot(1000,this, SLOT(shutdownlabel())); mtime= QDateTime::currentMSecsSinceEpoch();
}
Widget::~Widget()
{
delete ui;
}void Widget::shutdownlabel()
{qint64 Newtime = QDateTime::currentMSecsSinceEpoch() - mtime; //PRINT_INFO<<"newtime: " << Newtime/1000; Timeremain = TIMEOUT - (Newtime/1000); QLabel *label = new QLabel(ui->frame_2); label->setStyleSheet("border:1px solid black;text-align:cetre;"); label->setGeometry(50,1,202,20); label->clear(); label->setText("Timer:" +QString::number(Timeremain)); label->show(); QTimer::singleShot(1000,this, SLOT(shutdownlabel()));
}
-
when i use this dynamic label in slot its slot create n label at same place and so its get overlapped to avoid this i need to take dynamic label as global and clear it every time
-
Use layouts (subclasses of QLayout) instead of manual positioning (setGeometry()).
-
when i use this dynamic label in slot its slot create n label at same place and so its get overlapped to avoid this i need to take dynamic label as global and clear it every time
-
That sounds like a very wasteful approach... If you need a label there, just insert it upfront. If it does not need to be visible - hide it. If it needs to be visible - show it. If it's text needs to be changed, call
setText()
in your slot.There is no need to keep adding/ deleting labels, or clearing a global one etc.
-
This post is deleted!