PDA

View Full Version : QHBoxLayout



VitaliBR
4th December 2010, 20:50
I am doing the following to add my class (directX) in a QHBoxLayout:


Window::Window(QWidget* parent)
: QWidget(0) // top-level, no parent
{
ui.setupUi(this);

QHBoxLayout *layout = new QHBoxLayout();
setLayout(layout);
layout->addWidget(new QD3DWidget(this));
}


Everything works perfectly, but would like to use the QT Designer,
So I created a QHBoxLayout but I can not add in my QD3DWidget QHBoxLayout, I'm trying something like:

Window::Window(QWidget* parent)
: QWidget(0) // top-level, no parent
{
ui.setupUi(this);

ui.horizontalLayout->addWidget(new QD3DWidget(this));
}


The viewport directx does not appear, only a white window appears, and I set to be blue in directX,and when I make the code directly (without ui) it is blue

Thanks

johnc
4th December 2010, 21:17
Go into the generated ui file (probably ui_Window.h) to see what the difference is between that and your coded creation of the layout. This is how I usually figure out how to code something up that I can only get to work from designer. Something must be different between the two if they work differently...

VitaliBR
4th December 2010, 21:27
my ui_Main.h

/************************************************** ******************************
** Form generated from reading UI file 'Main.ui'
**
** Created: Sat 4. Dec 17:22:16 2010
** by: Qt User Interface Compiler version 4.7.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
************************************************** ******************************/

#ifndef UI_MAIN_H
#define UI_MAIN_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Form
{
public:
QWidget *horizontalLayoutWidget;
QHBoxLayout *horizontalLayout;

void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(909, 711);
horizontalLayoutWidget = new QWidget(Form);
horizontalLayoutWidget->setObjectName(QString::fromUtf8("horizontalLayoutWidget"));
horizontalLayoutWidget->setGeometry(QRect(90, 209, 381, 351));
horizontalLayout = new QHBoxLayout(horizontalLayoutWidget);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);

retranslateUi(Form);

QMetaObject::connectSlotsByName(Form);
} // setupUi

void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAIN_H

VitaliBR
5th December 2010, 01:54
anyone? :(

VitaliBR
5th December 2010, 23:57
As the window, I am creating in QtDesigner:
http://img600.imageshack.us/img600/3123/capturaru.png

Using the ui, the code looks like, and the window when run like this:

Window::Window(QWidget* parent)
: QWidget(parent)
{
ui.setupUi(this);

ui.horizontalLayout->addWidget(new QD3DWidget());

}
http://img703.imageshack.us/img703/4274/capturar2q.png

And when I create my QHBoxLayout the code looks like this:

Window::Window(QWidget* parent)
: QWidget(parent)
{
ui.setupUi(this);


QHBoxLayout *layout = new QHBoxLayout();
setLayout(layout);
layout->addWidget(new QD3DWidget());
}
http://img404.imageshack.us/img404/1097/capturar3b.png

A note: The blue screen means that the code of DirectX is working, I configured it to clean the screen with the color blue

and using the code (last image), so if I do (wrong):

layout->addWidget(new QD3DWidget()); //not using this

it is white
and if I do so, it turns blue (correct):

layout->addWidget(new QD3DWidget(this)); //using this


Thanks

ChrisW67
6th December 2010, 02:54
If you do not set the parent of the widget you are inserting into the layout then it cannot appear as a child of the form widget. A widget without a parent is a top-level window regardless of which layouts you add it to. It doesn't show because it has not inherited the visibility of its parent (it doesn't have one).

To do this with Designer:

Drag a generic QWidget into the form widget
Promote that widget to QD3DWidget (from right-click menu) http://doc.trolltech.com/4.7/designer-using-custom-widgets.html
Set the form widget layout to horizontal (it's on the Designer tool bar)
http://doc.trolltech.com/4.7/designer-layouts.html
Let Designer write the code for you


BTW: The layout you have dropped into your QWidget in Designer is unused.

VitaliBR
6th December 2010, 12:09
thanks guy!! worked perfectly :)

Only want to know how to access the methods of my class QD3DWidget :)

ChrisW67
6th December 2010, 21:03
The same way you access the methods of any other widget in the UI. It varies with how you have incorporated the Designer generated code, but generally:


ui->myWidget->myWidgetMethod();
or
ui.myWidget->myWidgetMethod();

The generated code will include the headers you specified when you promoted the QWidget, making your widget's methods known.