PDA

View Full Version : Operation of 'show'



sterling
8th January 2014, 22:44
I am creating a widget that is similar to another created in a different GUI.
If I do basewin->show() in the constructor, I get what I expect to see, the widget is displayed.
The constructor code is below.

ProgressBar::ProgressBar( QWidget *parent )
: QWidget(parent)
{
QRect newrct;
QRect qrct = parent->geometry();
basewin = new QDialog( this, Qt::Dialog );

if( parent == NULL )
{ basewin->setGeometry( QRect( BW_TOP, BW_LEFT, BW_BOTTOM - BW_TOP, BW_RIGHT - BW_LEFT ) ); }
else
{
// Set relative to the container
//
newrct.setTop( 0 );
newrct.setHeight( BW_BOTTOM - BW_TOP );
newrct.setLeft( 0 );
newrct.setWidth( BW_RIGHT - BW_LEFT );

basewin->setGeometry( newrct );
basewin->move( QPoint( qrct.left() + 20, qrct.top() + 20 ) );
}

QProgressBar *mybar = new QProgressBar( basewin );
mybar->setGeometry( QRect( 10, 30, (BW_RIGHT - BW_LEFT), 25 ) );

basewin->show();
}

If I comment out the basewin->show() in the constructor and enable the pb.show() as below in the application test, the widget doesn't display.
void TestRig::TestProgress( void )
{
ProgressBar pb( this );

pb.SetWindowTitle( "Progress bar title" );
// pb.show();

xvt_dm_post_note( "Test Progress: Any Key To Continue" );
}

What am I missing here. Should not the pb.show() in the application cause the widget to display.
The reason I need this behavior is that there are other items that have to be setup before the pb widget is displayed. Other controls may or may not be inserted and the geometry needs to be adjusted prior to the actual display.

Thanks

ChrisW67
8th January 2014, 23:49
It will cause the empty ProgressBar QWidget to display. The other widgets you create in the ProgressBar constructor are not put in a layout and do not automatically inherit visibility as a result. See: Layout Management

It's not immediately apparent what effect you are trying to achieve but the way you are going about it is irregular.

sterling
9th January 2014, 01:02
I don't quite understand what you are saying. In the TestProgress method, the base screen widget and the QProgressBar have been created. The base dialog window doesn't show up when show is called from outside the constructor. If just the QProgress bar didn't show up I could figure that but in fact nothing shows up. I looked at the layout management doc and in glancing at it, it doesn't appear to address what I need or the question of why show doesn't work from the outside.

This is a copy of an element that I wrote years ago in a different GUI system. The current product is being converted to Qt and this widget needs three QProgress bars, a QTextEdit, and several QLabels, and a QButton. Some or none of the extra elements may be displayed (and the geometry modified accordingly). What is displayed depends on the needs of the function calling it.

ChrisW67
9th January 2014, 01:54
The thing you are calling show() on is the the QWidget-derived ProgressBar object: and it will be shown. That object has created a few other objects that it owns (a generic QDialog and QProgressBar) but as it never puts them in a layout in its client area or explicitly show()s them these objects have no visible manifestation. Ownership does not equal visibility.

What you want is more like this:


#include <QtGui>

class ProgressDialog: public QDialog
{
Q_OBJECT
public:
explicit ProgressDialog(QWidget *p = 0): QDialog(p) {
QLabel *label = new QLabel("A Label", this);
QTextEdit *edit = new QTextEdit(this);
QProgressBar *pb1 = new QProgressBar(this);
pb1->setValue(10);
QProgressBar *pb2 = new QProgressBar(this);
pb2->setValue(50);
QProgressBar *pb3 = new QProgressBar(this);
pb3->setValue(90);
QDialogButtonBox *bb = new QDialogButtonBox(this);
bb->addButton(QDialogButtonBox::Cancel);
bb->addButton(QDialogButtonBox::Ok);
connect(bb, SIGNAL(rejected()), SLOT(reject()));
connect(bb, SIGNAL(accepted()), SLOT(accept()));

QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(label);
layout->addWidget(edit);
layout->addWidget(pb1);
layout->addWidget(pb2);
layout->addWidget(pb3);
layout->addWidget(bb);
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

ProgressDialog pb;
pb.show();
return app.exec();
}
#include "main.moc"

sterling
9th January 2014, 05:12
Thanks. Most important was understanding what was happening, whether or not it was the 'right way' was of secondary importance. FYI, I created a virtual void show( void ) in the class and when show was called from the outside, it did what I expected it to do in the first place.

I will look at the example to understand what you did and its ramifications with respect to what I am trying to do. If there was any name confusion, I need to keep the class name the same to avoid having to change the source code in the older GUI.

Thanks