PDA

View Full Version : how to show and hide Qlabel continuously on mainwindow



prasad1001
29th March 2014, 07:59
Hello all,
I am new to Qt, please tell me how to show and hide the Qlabel message continuously on my mainwindow...

Here is my code..

MainWindow.cpp


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
label=ui->label;

display();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::display()
{
while(1)
{
label->show();
sleep(1000);
label->hide();
}
}

when i run the program mainwindow is not comming...and no errors are comming..
Please help me..
Thank you.

Lesiok
29th March 2014, 08:09
First of all loop in MainWindow::display is "never ending story". Thus MainWindow constructor is blocking application.
Second UI need working event loop but You are blocking her. Read about QTimer.

prasad1001
29th March 2014, 10:15
Thank you for the reply....can u please provide with an example code..
I didn't get ur point..

Thanks in advance..

Lesiok
29th March 2014, 13:54
Method display should be defined as slot and looks like :
void MainWindow::display()
{
label->setVisible(!label->isVisible());// Set the status of the opposite
QTimer::singleShot(500,this,SLOT(display()));// call display() after 500 ms
}

prasad1001
31st March 2014, 08:54
Method display should be defined as slot and looks like :
void MainWindow::display()
{
label->setVisible(!label->isVisible());// Set the status of the opposite
QTimer::singleShot(500,this,SLOT(display()));// call display() after 500 ms
}

Thanks for the reply sir..

I was placed that method...but when i run my program..it is showing that..
"The program has unexpectedly finished."

Thanks in advance..

ChrisW67
31st March 2014, 09:10
Run your program in your debugger. When it crashes look at where it crashed in the back trace you will likely be given.