[SOLVED] QLabel->isVisible always returns false
-
If you are using the above statement inside the constructor it will always result false. Calling setVisible(true) will set the widget to be visible if all its parents widget up to the window are visible.
Once the mainwindow/parent is visible only then the label visible property is changed to true.
Check "this":http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#visible-prop for more information.
-
hello l3e0wulf, I executed the following code:
Inside the constructor class:
MyLabel = new QLabel(this);
MyLabel->setVisible(true);
qDebug() << MyLabel->isVisible();in the main function:
{
QApplication a(argc, argv);
MainWindow w;
w.show();
qDebug() << w.MyLabel->isVisible();return a.exec();
}Result:
false
true -
bq. If you are using the above statement inside the constructor it will always result false. Calling setVisible(true) will set the widget to be visible if all its parents widget up to the window are visible.
Once the mainwindow/parent is visible only then the label visible property is changed to true.
Check this [qt-project.org] for more information.I can't understand how fix it.
@MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{MyLabel = new QLabel(this);
MyLabel ->setVisible(true);
qDebug() << MyLabel ->isVisible();}@
-
Its simple the parent of the MyLabel is MyWidget, and at the start MyWidget is not visible. So inside the main you write
@MyWidget w;
w.show(); @When MyWidget is initialised (first line) it call its constructor, and inside the constructor you have initialised MyLabel and set its property setVisible to "true", and the next line is qDebug() , now according to the documentation :
bq. "Calling setVisible(true) will set the widget to be visible if all its parents widget up to the window are visible"
So MyWidget is not yet visible unless w.show() is called. Hence when the parent becomes visible then the property of the child setVisible(true) is reflected.
-
Still don't working.
@MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this); MyWidget *MWidget = new MyWidget(this); MyWidget->setGeometry(QRect(0, 0, 336, 425)); MyWidget->show();
}
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{MyLabel = new QLabel(this);
MyLabel ->setVisible(true);
qDebug() << MyLabel ->isVisible();}@
-
This wont work like this , See when you initialise
@MyWidget *MWidget = new MyWidget(this);@
it calls MyWidget constructor i.e
@MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
MyLabel = new QLabel(this);MyLabel ->setVisible(true);
qDebug() << MyLabel ->isVisible();
}@
and till this point MyWidget.show() is not called or MyWidget is not visible so output will be false (inside the constructor). Just for test you can call show() inside the constructor and check the two behaviour.
@MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{show();
MyLabel = new QLabel(this);
MyLabel ->setVisible(true);
qDebug() << MyLabel ->isVisible();
}@
-
Still does not work, only works if I put show () inside the constructor MainWindow (); example:
@MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this); show(); MyWidget *MWidget = new MyWidget(this);
}
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{show();
MyLabel = new QLabel(this);
MyLabel ->setVisible(true);
qDebug() << MyLabel ->isVisible();
}@
The funny thing is that in the main.cpp the MainWidget already with show().