PDA

View Full Version : convoluted GUI



Walsi
25th April 2007, 14:53
Got a problem; look:

main.cpp

#include <QApplication>

#include "usc_sip_api.h"
#include "usc_gui.h"
#include "usc_gateway.h"

GatewayUnit *uscGatewayUnit = new GatewayUnit(0);

int main(int argc, char *argv[])
{
init_sip();

QApplication uscApp(argc, argv);
UscGuiES1 uscGui;
uscGui.show();

return uscApp.exec();

}

usc_gui.cpp

#include <QPalette>
#include <QtGui>

#include "usc_gui.h"
#include "usc_radio_element.h"
#include "usc_gateway.h"

UscGuiES1::UscGuiES1(QWidget *parent)
: QWidget(parent)
{
UscRadioElement uscRadioElement;
uscRadioElement.show();

setWindowTitle(tr("Universal SIP Client"));
}

usc_radio_element.cpp

#include <QPalette>
#include <QtGui>

#include "usc_gui.h"
#include "usc_radio_element.h"

UscRadioElement::UscRadioElement(QWidget *parent)
: QWidget(parent)
{
radioButton = new QPushButton(tr("R1"));
rxButton = new QPushButton(tr("Rx"));
txButton = new QPushButton(tr("Tx"));
pttButton = new QPushButton(tr("PTT"));

QVBoxLayout *rxtxLayout = new QVBoxLayout;
rxtxLayout->addWidget(rxButton);
rxtxLayout->addWidget(txButton);

QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(radioButton);
topLayout->addLayout(rxtxLayout);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addWidget(pttButton);

setLayout(mainLayout);
}

I got no exception at compiling but my window is empty when I start the application.

What is my mistake, what can I do to solve this problem?


Yours Sincerely
Walsi

marcel
25th April 2007, 15:00
After setlayout( mainLayout ), try setting setFixedSize( sizeHint() );
Just for curiosity, to see if this is the reason - the widget doesn't get resized yet.

Then you'll know what the problem is.

Regards

marcel
25th April 2007, 15:01
No, forget that :)
You create your buttons without a parent.
Pass the "this" pointer as parent and everything will be OK.

Regards

jpn
25th April 2007, 15:32
Layouts handle reparenting so it should be fine.

The reason is likely this:


UscGuiES1::UscGuiES1(QWidget *parent)
: QWidget(parent)
{
UscRadioElement uscRadioElement; // a local variable is destroyed when going out of scope
uscRadioElement.show();

setWindowTitle(tr("Universal SIP Client"));
}

Walsi
26th April 2007, 09:01
@ marcel: please explain a little bit more detaled what you are meaning

@ jpn: yes and how can I handle my problem that it works???

Best Regards,...

jpn
26th April 2007, 09:11
It depends how you want it to be. Here's one way:


UscGuiES1::UscGuiES1(QWidget *parent)
: QWidget(parent)
{
UscRadioElement* uscRadioElement = new UscRadioElement(this);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(uscRadioElement);

setWindowTitle(tr("Universal SIP Client"));
}

Walsi
26th April 2007, 09:20
That's exactly the solution I want. Many thanks to you!

But my further questions, ... to come away from my newbie status, ... are following:

Can you explain this 3 little code lines a liitle bit so that I understand it???

Why do you define uscRadioElement as a Pointer and not like this?


UscRadioElement uscRadioElement:

What means (this) at the uscRadioElement Definition and also at the QVBoxLayout definition??

Best Regards!

jpn
26th April 2007, 09:53
Can you explain this 3 little code lines a liitle bit so that I understand it???

Why do you define uscRadioElement as a Pointer and not like this?


UscRadioElement uscRadioElement:


A local variable allocated on the stack no more exists after the block finishes:


[...]
{
UscRadioElement uscRadioElement;
[...]
} // object "uscRadioElement" no more exists


However,

[...]
{
UscRadioElement* uscRadioElement = new UscRadioElement(this);
[...]
} // the pointer "uscRadioElement" no more exists, but the actual object remains alive




What means (this) at the uscRadioElement Definition and also at the QVBoxLayout definition??
It passes "this" as a parent for the layout. Refer to docs (http://doc.trolltech.com/4.2/objecttrees.html) about QObject's parent-child relationship. When a layout is in question, it also has a special meaning that it basically calls setLayout() on the passed parent for you.

Walsi
26th April 2007, 10:15
thanks, that is enough explanation for me! :rolleyes:

Best Regards!