PDA

View Full Version : How to combine two QWidgets(tracking widget with console) & show them in QMainwindow



liziyuanhaha
25th August 2015, 19:20
My codes was derived from Qt example "Terminal".

I added another class "trackingWidget", and it works very well as an independent widget from QMainWindow, where the console stays.

Now I am trying to put my "trackingWidget" on the right side of console, and keep both of them inside QMainWindow; however, my "trackingWidget" disappeared.
Here is my code:



console = new Console;
console ->setEnabled(false);

QGridLayout *centralLayout = new QGridLayout;
centralLayout->addWidget(console , 0,0,1,1);
centralLayout->addWidget(trackingWidget, 0,1,1,1);
trackingWidget->resize(512,512);
console ->resize(325,512);

QWidget *myCentralWidget = new QWidget;
myCentralWidget->setLayout(centralLayout);
setCentralWidget(myCentralWidget);



It is ok if I just add trackingWidget; however, once I add console, the trackingWidget would be crushed to nowhere like it has never been added.

Is it possible to show multi-QWidgets with flexible sizes in one QWidget/QMainWindow?

Thanks!

anda_skoa
25th August 2015, 21:06
What is that trackingWidget?
Is it a composition of other widgets?
If so, is it properly layouted?
If not, does it have a sizeHint()?

Cheers,
_

Radek
25th August 2015, 21:30
You'd better devise your UI in the Designer: this way you save yourself most of troubles with positioning and sizing widgets. Both the console and the trackingWidget are descendant of some standard parts of Qt: for example,the console is derived from QTextEdit most likely. Thus, create a main window in the Designer, add a QTextEdit, add a widget, which is the base of trackingWidget, set sizes, and put both in a horizontal layout. Set spacing of the layout and promote your widgets to console and the trackingWidget (all of this is made in the Designer). Save. Getting your UI to the screen is a matter of two simple calls: setupUi() and show().