PDA

View Full Version : QGLWidget problem



MarkoSan
7th December 2007, 18:01
Hello to all!

I've been trying for whole day to make following demand work:

I have a QDockWidget on QMainWIndow. In this QDockWIdget I want to have an QGLWidgeet containing some animation and beneath in horiz layout two QPushButtons.

I wrote following code:
COperationWIndow::COperationWIndow(QWidget* pParent) : QDockWidget(pParent)
{
//setAllowedAreas(Qt::LeftDockWidgetArea); // sets allowed dock area
setWindowTitle(trUtf8("Izbor ponudbe jedi in pijač")); // sets window title

// layout creation
m_pVLayout=new QVBoxLayout(this); // creates new vertical layout
Q_CHECK_PTR(m_pVLayout); // checks creation
m_pHLayout=new QHBoxLayout(this); // creates new horizontal layout
Q_CHECK_PTR(m_pHLayout); // checks creation

// creates merchandize browser
m_pMerchandizeBrowser=new CMerchandizeBrowser(this); // creates new merchandize selector
//m_pMerchandizeBrowser=new CMerchandizeBrowser(pParent); // creates new merchandize selector
Q_CHECK_PTR(m_pMerchandizeBrowser); // checks creation
m_pVLayout->addWidget(m_pMerchandizeBrowser); // adds merchandize selector to vertical layout

// creates Left Selector pushbutton
m_pLeftButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira levega artikla"), this);
//m_pLeftButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira levega artikla"), pParent);
Q_CHECK_PTR(m_pLeftButtonMerchandizeSelector); // checks creation

// creates Right Selector pushbutton
m_pRightButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira desnegs artikla"), this);
//m_pRightButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira desnegs artikla"), pParent);
Q_CHECK_PTR(m_pRightButtonMerchandizeSelector); // checks creation

m_pHLayout->addWidget(m_pLeftButtonMerchandizeSelector); // adds button to horiz. layout
m_pHLayout->addWidget(m_pRightButtonMerchandizeSelector); // adds button to horiz. layout

m_pVLayout->addLayout(m_pHLayout); // add horiz. layout to vertical layout
setLayout(m_pVLayout); // sets main layout on widget
setLayout(m_pHLayout); // sets main layout on widget
//setWidget(m_pMerchandizeBrowser);
}

but this does not work. QGLWidget is not even seen, both qpushbuttons are located in the left upper corner one behind other. What the heck is going on???!!

jacek
7th December 2007, 18:21
setLayout(m_pVLayout); // sets main layout on widget
setLayout(m_pHLayout); // sets main layout on widget

You can't set the layout twice.

MarkoSan
7th December 2007, 18:29
I've commented the line:
setLayout(m_pHLayout); // sets main layout on widget and I get same result.

high_flyer
7th December 2007, 18:30
That is actually an interesting point - since I don't have the time to make a test program, can you answer me quick jacek, as to how does the implementation looks like when I have two layouts on a widget lets say one V box and one H box with buttons in them, all done by designer?
Will designer generate a third layout manager to inlcude the ones I added?

high_flyer
7th December 2007, 18:31
and I get same result.
Thats because only one layout is visible.
You need to create one layout that will include the other two.
And set the that layout (which includes the others) to the widget.

MarkoSan
7th December 2007, 18:36
I've created two layouts, as is seen from code. Vertical layout is main layout, since in this layout in the upper region should be qglwidget, and beneath the qglwidget should be in horizontal layoout two push buttons.

high_flyer
7th December 2007, 18:38
in that case so include the horizontal laout in the vertical one, and set the vertical as the main layout (as you did).

MarkoSan
7th December 2007, 18:39
I did that, but I still get same result.

high_flyer
7th December 2007, 18:42
can you show your current code?

jacek
7th December 2007, 18:45
how does the implementation looks like when I have two layouts on a widget lets say one V box and one H box with buttons in them, all done by designer?
You can add a layout to another one, just like you do with widgets, and you always have a single top-level layout.

MarkoSan
7th December 2007, 18:46
Here is it:
#include "COperationWIndow.h"

COperationWIndow::COperationWIndow(QWidget* pParent) : QDockWidget(pParent)
{
//setAllowedAreas(Qt::LeftDockWidgetArea); // sets allowed dock area
setWindowTitle(trUtf8("Izbor ponudbe jedi in pijač")); // sets window title

// layout creation
m_pVLayout=new QVBoxLayout(this); // creates new vertical layout
Q_CHECK_PTR(m_pVLayout); // checks creation
m_pHLayout=new QHBoxLayout(this); // creates new horizontal layout
Q_CHECK_PTR(m_pHLayout); // checks creation

// creates merchandize browser
m_pMerchandizeBrowser=new CMerchandizeBrowser(this); // creates new merchandize selector
//m_pMerchandizeBrowser=new CMerchandizeBrowser(pParent); // creates new merchandize selector
Q_CHECK_PTR(m_pMerchandizeBrowser); // checks creation
m_pVLayout->addWidget(m_pMerchandizeBrowser); // adds merchandize selector to vertical layout

// creates Left Selector pushbutton
m_pLeftButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira levega artikla"), this);
//m_pLeftButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira levega artikla"), pParent);
Q_CHECK_PTR(m_pLeftButtonMerchandizeSelector); // checks creation

// creates Right Selector pushbutton
m_pRightButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira desnegs artikla"), this);
//m_pRightButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira desnegs artikla"), pParent);
Q_CHECK_PTR(m_pRightButtonMerchandizeSelector); // checks creation

m_pHLayout->addWidget(m_pLeftButtonMerchandizeSelector); // adds button to horiz. layout
m_pHLayout->addWidget(m_pRightButtonMerchandizeSelector); // adds button to horiz. layout

m_pVLayout->addLayout(m_pHLayout); // add horiz. layout to vertical layout
setLayout(m_pVLayout); // sets main layout on widget
}

COperationWIndow::~COperationWIndow()
{
}

high_flyer
7th December 2007, 18:50
@MarkoSan:
What happens if you do:

m_pHLayout=new QHBoxLayout(m_pVLayout);
Ah no... QLayout is not a widget...
Hmm I need to look this up...

@jacek:

and you always have a single top-level layout.
No, when you use designer, you can have multiple "flowing" layouts on one widget, which is why I asked my question.

jacek
7th December 2007, 19:13
you can have multiple "flowing" layouts on one widget, which is why I asked my question.
These "flowing layouts" are QWidgets. In such case the main widget doesn't have any layouts set.

high_flyer
7th December 2007, 19:20
These "flowing layouts" are QWidgets. In such case the main widget doesn't have any layouts set.
Well, not according to the designer object browser on the right.
Try it:
Place 4 buttons on a widget.
Select 2 of the buttons, and but them in a VBox layout.
The other 2 on a another layout.
No new widgets are to be seen on the object manager.
That was my question - how designer implements this case.
But I find it hard to beive that it create a new widgets for the layouts...
See image.

high_flyer
7th December 2007, 19:54
Ok I uic'ed the ui and you arr right jecek, the uic actually cretes a widget for each layout.
Here is the uic generated code:


Dialog->resize(400, 300);
widget = new QWidget(Dialog);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(100, 40, 162, 27));
hboxLayout = new QHBoxLayout(widget);
hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
hboxLayout->setContentsMargins(0, 0, 0, 0);
pushButton = new QPushButton(widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));

hboxLayout->addWidget(pushButton);

pushButton_2 = new QPushButton(widget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));

hboxLayout->addWidget(pushButton_2);

widget1 = new QWidget(Dialog);
widget1->setObjectName(QString::fromUtf8("widget1"));
widget1->setGeometry(QRect(90, 170, 79, 58));
vboxLayout = new QVBoxLayout(widget1);
vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
vboxLayout->setContentsMargins(0, 0, 0, 0);
pushButton_3 = new QPushButton(widget1);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));

vboxLayout->addWidget(pushButton_3);

pushButton_4 = new QPushButton(widget1);
pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));

MarkoSan
7th December 2007, 20:02
And what about me? :rolleyes:

high_flyer
7th December 2007, 20:03
@MarcoSan:
I don't want you to think I hijacked your thread.
This is all related actually.
Here is a uic generated code for exatly what you are trying to do.
Instead of your GLWidget I use a QLabel.
Copy this implementation (not the code) and I bet yours will work as well (if you do it correctly):


widget = new QWidget(Dialog); //your QDocWidget

widget->setGeometry(QRect(20, 50, 164, 49));

vboxLayout = new QVBoxLayout(widget);

vboxLayout->setContentsMargins(0, 0, 0, 0);

label = new QLabel(widget); //your QGLWidget

vboxLayout->addWidget(label);

hboxLayout = new QHBoxLayout();

pushButton = new QPushButton(widget);

hboxLayout->addWidget(pushButton);

pushButton_2 = new QPushButton(widget);

hboxLayout->addWidget(pushButton_2);


vboxLayout->addLayout(hboxLayout);

MarkoSan
7th December 2007, 20:07
I am lookng at code chunk you posted and I concentrate on layouts. I have the same layout implementation as you. Is it possible QGLWidget cannot be placed on QDockWidget?

And what is the purpose of setObjectName method in your code? What are you trying to achieve with that?

high_flyer
7th December 2007, 20:20
Try this:


m_pHLayout=new QHBoxLayout(/*this*/);

high_flyer
7th December 2007, 20:23
A QDockWidget acts as a wrapper for its child widget, set with setWidget().
May be you should try to create a QWidget, put all your stuff with the layouts on it, and add that widget to the dock widget.

MarkoSan
7th December 2007, 20:28
Is it possible to place all controls I need directly to QMainWIndow? So there is no more need of any "foundation" widgets?

jacek
7th December 2007, 21:10
Is it possible to place all controls I need directly to QMainWIndow? So there is no more need of any "foundation" widgets?
No, QMainWindow has a special layout that you can't change, but you can use QWidget instead.

MarkoSan
7th December 2007, 23:34
No, QMainWindow has a special layout that you can't change, but you can use QWidget instead.

So, I try following:

On QMainWindow, I create QWidget and then I setup it as QMainWindow's central widget?

jacek
7th December 2007, 23:42
On QMainWindow, I create QWidget and then I setup it as QMainWindow's central widget?
Yes, of course the main window takes the ownership of this widget, so you don't have to care about its deletion.

MarkoSan
7th December 2007, 23:45
So, I try following:

On QMainWindow, I create QWidget and then I setup it as QMainWindow's central widget?

I did that and it WORKS! :D:D:D:D

Thanks a lot to all of you

MarkoSan
8th December 2007, 07:28
Well, I simply do not get it anymore. Here is the code. Now I want to add an QTableWidget near QGLWidget and I get same problem over again. I add to layout using:
m_pVLayout->addLayout(m_pHLayout); // add horiz. layout to vertical layout
m_pMainLayout->addLayout(m_pVLayout); // adds vert. layout to main layout
m_pMainLayout->addWidget(m_pMerchandizeTable); // adds merchandize table to layout
//m_pMainLayout->addWidget(pTWidget); // test
//setLayout(m_pVLayout); // sets main layout on widget
setLayout(m_pMainLayout); // sets main layout on widget

and table widget is visibible, but in upper left corner again. Why????!!!

jacek
8th December 2007, 15:26
What widget do you call the setLayout() method on? And why can't you use Qt Designer?

MarkoSan
8th December 2007, 15:37
Well, setLayout is called on class COperationWindow (derived from QWidget), which contains QGLWidget, QLabel and three QPusbuttons. Here is a code:
#include "COperationWIndow.h"

COperationWIndow::COperationWIndow(QWidget* pParent) : QWidget(pParent)
//COperationWIndow::COperationWIndow(QWidget* pParent) : QDockWidget(pParent)
{
//setAllowedAreas(Qt::LeftDockWidgetArea); // sets allowed dock area
//setWindowOpacity(rDefaultWindowOpacity); // sets window opacity
setWindowTitle(trUtf8("Jedilni list")); // sets window title

QFont m_cFont("1942 report", 72, QFont::Bold); // sets up font
//QFont m_cFont("Mom´sTypewriter", 72, QFont::Bold); // sets up font
//m_cFont.setStretch(QFont::UltraCondensed); // sets strech factor

// sets black background for this window
setAutoFillBackground(true);
m_cPalette=this->palette(); // gets current pallete of current widget
m_cPalette.setColor(QPalette::Background, Qt::black); // sets background color of widget
this->setPalette(m_cPalette);

// layout creation
m_pVLayout=new QVBoxLayout(this); // creates new vertical layout
Q_CHECK_PTR(m_pVLayout); // checks creation
m_pHLayout=new QHBoxLayout(this); // creates new horizontal layout
Q_CHECK_PTR(m_pHLayout); // checks creation
m_pMainLayout=new QHBoxLayout(this); // creates main layout
Q_CHECK_PTR(m_pMainLayout); // checks creation

// creates merchandize browser
m_pMerchandizeBrowser=new CMerchandizeBrowser(this); // creates new merchandize selector
//m_pMerchandizeBrowser=new CMerchandizeBrowser(pParent); // creates new merchandize selector
Q_CHECK_PTR(m_pMerchandizeBrowser); // checks creation

// creates Left Selector pushbutton
m_pLeftButtonMerchandizeSelector=new QPushButton(trUtf8("Levo"), this);
//m_pLeftButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira levega artikla"), pParent);
Q_CHECK_PTR(m_pLeftButtonMerchandizeSelector); // checks creation
//m_pLeftButtonMerchandizeSelector->setFlat(true); // sets flat flag
// sets new size
//m_pLeftButtonMerchandizeSelector->resize(iMerchandizeButtonSelectorWidth, iMerchandizeButtonSelectorHeight);
connect(m_pLeftButtonMerchandizeSelector,
SIGNAL(clicked()),
this,
SLOT(showLeftMerchandize()));

// creates merchandizer confirmer
m_pButtonMerchandizeConfirmer=new QPushButton(trUtf8("IZBERI"), this);
Q_CHECK_PTR(m_pButtonMerchandizeConfirmer);
connect(m_pButtonMerchandizeConfirmer,
SIGNAL(clicked()),
this,
SLOT(chooseMerchandize()));

// creates Right Selector pushbutton
m_pRightButtonMerchandizeSelector=new QPushButton(trUtf8("Desno"), this);
//m_pRightButtonMerchandizeSelector=new QPushButton(trUtf8("Izbira desnegs artikla"), pParent);
Q_CHECK_PTR(m_pRightButtonMerchandizeSelector); // checks creation
//m_pRightButtonMerchandizeSelector->setFlat(true); // sets flat flag
// sets new size
//m_pRightButtonMerchandizeSelector->resize(iMerchandizeButtonSelectorWidth, iMerchandizeButtonSelectorHeight);
connect(m_pRightButtonMerchandizeSelector,
SIGNAL(clicked()),
this,
SLOT(showRightMerchandize()));

// left button color setup
m_cPalette=m_pLeftButtonMerchandizeSelector->palette(); // gets current pallete of button "Left"
m_cPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
m_cPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
m_pLeftButtonMerchandizeSelector->setPalette(m_cPalette); // sets new pallete
m_pLeftButtonMerchandizeSelector->setFont(m_cFont); // sets new font

// confirmer button color setup
m_cPalette=m_pButtonMerchandizeConfirmer->palette(); // gets current pallete of button "Left"
m_cPalette.setColor(QPalette::Button, Qt::blue); // sets up new palette componenet
m_cPalette.setColor(QPalette::ButtonText, Qt::black); // sets up new palette componenet
m_pButtonMerchandizeConfirmer->setPalette(m_cPalette); // sets new pallete
m_pButtonMerchandizeConfirmer->setFont(m_cFont); // sets new font

// right button color setup
m_cPalette=m_pRightButtonMerchandizeSelector->palette(); // gets current pallete of button "Right"
m_cPalette.setColor(QPalette::Button, Qt::black); // sets up new palette componenet
m_cPalette.setColor(QPalette::ButtonText, Qt::white); // sets up new palette componenet
m_pRightButtonMerchandizeSelector->setPalette(m_cPalette); // sets new pallete
m_pRightButtonMerchandizeSelector->setFont(m_cFont); // sets new font

// TODO: show selected merchandize name and price

// table of ordered merchandize creation
/*
m_pMerchandizeTable=new QTableWidget(this); // creates new table
Q_CHECK_PTR(m_pMerchandizeTable); // checks creation
m_pMerchandizeTable->setColumnCount(iNrColumns); // sets number of columns
m_pMerchandizeTable->setRowCount(iNrRows); // sets number of rows
*/

// selected merchandize name label widget
m_pMerchandizeNameLabel=new QLabel(this); // creates new label
Q_CHECK_PTR(m_pMerchandizeNameLabel); // checks creation
m_cPalette=m_pMerchandizeNameLabel->palette(); // geta label palette
m_cPalette.setColor(QPalette::Text, QColor(240, 167, 16)); // sets palette color
QFont labelFont("Rothenburg Decorative", 36, QFont::Bold); // sets up new font
m_pMerchandizeNameLabel->setFont(labelFont); // sets font
m_pMerchandizeNameLabel->setFrameStyle(QFrame::Sunken); // sets decoration
// sets selected merchandize name
m_pMerchandizeNameLabel->setText(m_pMerchandizeBrowser->getMerchandizeName(m_pMerchandizeBrowser->getSelected()));
m_pMerchandizeNameLabel->setAlignment(Qt::AlignCenter); // set text justification to "centering"
qDebug() << m_pMerchandizeBrowser->getMerchandizeName(m_pMerchandizeBrowser->getSelected());

// layouts setup
m_pHLayout->addWidget(m_pLeftButtonMerchandizeSelector); // adds button to horiz. layout
m_pHLayout->addWidget(m_pButtonMerchandizeConfirmer); // adds a button to horiz. layout
m_pHLayout->addWidget(m_pRightButtonMerchandizeSelector); // adds button to horiz. layout

m_pVLayout->addWidget(m_pMerchandizeBrowser); // adds merchandize selector to vertical layout
m_pVLayout->addWidget(m_pMerchandizeNameLabel); // adds merchandize name label to vertical layout
m_pVLayout->addLayout(m_pHLayout); // add horiz. layout to vertical layout
m_pMainLayout->addLayout(m_pVLayout); // adds vert. layout to main layout
//m_pMainLayout->addWidget(m_pMerchandizeTable); // adds merchandize table to layout
setLayout(m_pMainLayout); // sets main layout on widget

// creats new timer, binded to main application object
/*
QPointer<QTimer> m_pAdvertisementTimer=new QTimer(this);
Q_CHECK_PTR(m_pAdvertisementTimer); // checks creation
connect(m_pAdvertisementTimer, SIGNAL(timeout()), this, SLOT(startAdvertising()));
m_pAdvertisementTimer->start(iAdvertisingTimeout); // restarts advertisement timer
*/
}

void COperationWIndow::showLeftMerchandize()
{
m_iSelected=m_pMerchandizeBrowser->getSelected()-1; // selects left merchandize
m_pMerchandizeBrowser->setSelected(m_iSelected);
m_pMerchandizeNameLabel->setText(m_pMerchandizeBrowser->getMerchandizeName(m_pMerchandizeBrowser->getSelected()));
m_pMerchandizeNameLabel->setAlignment(Qt::AlignCenter); // set text justification to "centering"
m_pMerchandizeBrowser->update();
//m_pAdvertisementTimer->start(iAdvertisingTimeout); // restarts advertisement timer
}

void COperationWIndow::showRightMerchandize()
{
m_iSelected=m_pMerchandizeBrowser->getSelected()+1; // selects left merchandize
m_pMerchandizeBrowser->setSelected(m_iSelected);
m_pMerchandizeNameLabel->setText(m_pMerchandizeBrowser->getMerchandizeName(m_pMerchandizeBrowser->getSelected()));
m_pMerchandizeNameLabel->setAlignment(Qt::AlignCenter); // set text justification to "centering"
m_pMerchandizeBrowser->update(); // showa selected merchandize
//m_pAdvertisementTimer->start(iAdvertisingTimeout); // restarts advertisement timer
}

// slot for choosing merchandize
void COperationWIndow::chooseMerchandize()
{
}

// slot for showing advertisement banners
void COperationWIndow::startAdvertising()
{
}

COperationWIndow::~COperationWIndow()
{
}I do not use designet because I simply do not like it. And by the way, Qlabel DOES NOT show any text and this is simply driving me mad right now. And I know I've momentaly commented the code regarding QTableWidget, it's for testing purposes only, nevertheless, once qtablewidget code is uncommented the problem remains.

jacek
8th December 2007, 16:08
m_pVLayout=new QVBoxLayout(this); // creates new vertical layout
Q_CHECK_PTR(m_pVLayout); // checks creation
m_pHLayout=new QHBoxLayout(this); // creates new horizontal layout
Q_CHECK_PTR(m_pHLayout); // checks creation
m_pMainLayout=new QHBoxLayout(this); // creates main layout
Q_CHECK_PTR(m_pMainLayout); // checks creation
You shouldn't pass "this" to layouts' constructors, because they will call setLayout() on the parent.


I do not use designet because I simply do not like it.
Like it or not, but it would take you only a couple of minutes to set up this widget in Qt Designer.


And by the way, Qlabel DOES NOT show any text and this is simply driving me mad right now.
Because you should use QPalette::WindowText instead of QPalette::Text and you have to set the changed palette back on the label to see the effect.

MarkoSan
8th December 2007, 16:21
You shouldn't pass "this" to layouts' constructors, because they will call setLayout() on the parent.


Like it or not, but it would take you only a couple of minutes to set up this widget in Qt Designer.


Because you should use QPalette::WindowText instead of QPalette::Text and you have to set the changed palette back on the label to see the effect.

Jacek, thanks on tip on QLabel, it works now. But whenever I create Layouts with "this" parameter or without, result of placing QWIdgetTable is still the same. :confused:

jacek
8th December 2007, 16:26
whenever I create Layouts with "this" parameter or without, result of placing QWIdgetTable is still the same. :confused:
Is this what you are trying to achieve?

MarkoSan
8th December 2007, 16:30
Yessssssss! But whatever I do, I get same result: QTableWidget is in upper left corner and therefore this is NOT ok. I want result excatly as on your pic.

jacek
8th December 2007, 16:35
That's weird, because I've just uncommented the m_pMerchandizeTable lines (including the one with addWidget()). Run "make clean" and make sure you don't pass any parents to layout constructors.

MarkoSan
8th December 2007, 16:50
That's weird, because I've just uncommented the m_pMerchandizeTable lines (including the one with addWidget()). Run "make clean" and make sure you don't pass any parents to layout constructors.

I deleted manually all ..\debug\ files and now it works. I do not get it what was wrong. Jacek, thanks!!!