PDA

View Full Version : Dynamic widget not displayed upon Qwidget



ashukla
16th December 2007, 10:16
Hi!
I wants to put multiple dynamic QWidget inside QWidget widget. for this I am creating a QMainWindow and inside this I putted the Widget.
In ui_mainwindow.h the variable is like this

public:
QAction *actionRect;
QWidget *centralwidget;
QWidget *widget;
QLineEdit *lineEdit;
QPushButton *pushButton;
QPushButton *pushButton_2;
QStatusBar *statusbar;
QToolBar *toolBar;
and in void setupUi(QMainWindow *MainWindow) code is following;

centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
widget = new QWidget(centralwidget);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(90, 0, 721, 561));


//mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"

class MainWindow: public QMainWindow, public Ui_MainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
QWidget *wd[100];

private slots:
void newRect();

};
#endif // MAINWINDOW_H

After that I am calling newRect slot which is connected with QAction *actionRect like this in MainWindow class constructor.
//mainwindow.cpp

#include "mainwindow.h"

int num;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
num=1;

connect(actionRect, SIGNAL(triggered()), this, SLOT(newRect()));

}

void MainWindow::newRect()
{
wd[num] = new QWidget(this->centralwidget);
//wd[num]->setGeometry(5,50,30,30);

wd[num]->show();
lineEdit->setText( QString::number(wd[num]->height()));
num++;

}

My wd[num] widget is not displaying upon widget.
What shuld I do for this?

c_07
16th December 2007, 14:53
Perhaps create a QGridLayout inside the central widget, and add your sub-widgets to that?

http://www.screencast.com/t/UOZtygllkz
The screenshot is using the designer but you could do this dynamically in your code as well.

ashukla
17th December 2007, 08:03
Dear Experts!
I redefine the situation;
Firstly, I have putted a QWidget in a QMainWindow.
A QMainWindow make a QWidget as a centralwidget for self.
centralwidget is a child of QMainWindow & setted as a central window through Gui automatically. So that when I putted the QWidget widget inside QMainWindow It will be a child of centralwidget and so in turn is a child of QMainWindow.

I am comming on my problem;
I want to put dynammically QWidgets as a child of widget. So, I have make a newRect() slot for that. its code is following;

void MainWindow::newRect()
{
wd[num] = new QWidget(widget);
wd[num]->show();
lineEdit->setText( QString::number(wd[num]->height()));
num++;

}
There is no widgets displays inside widget. but in lineEdit QWidget wd[num] height displays.

If I am layouting the widget.
At runtime message appears Layout is already set.

How a way I show the widget?
Thanking in advance!
regards!

jacek
17th December 2007, 11:08
There is no widgets displays inside widget.
What makes you think that these widgets aren't shown?

Try:
wd[num] = new QPushButton( "aaa", widget );


If I am layouting the widget.
At runtime message appears Layout is already set.
How do you set that layout and on which widget?

ashukla
17th December 2007, 11:52
Dear Sir!

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>

class Ui_MainWindow
{
public:
QAction *actionRect;
QWidget *centralwidget;
QLineEdit *lineEditX;
QWidget *widget;
QLineEdit *lineEditY;
QLineEdit *lineEditHeight;
QLineEdit *lineEditWidth;
QLabel *labelX;
QLabel *labelY;
QLabel *labelHeight;
QLabel *labelWidth;
QStatusBar *statusbar;
QToolBar *toolBar;

void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(824, 600);
actionRect = new QAction(MainWindow);
actionRect->setObjectName(QString::fromUtf8("actionRect"));
actionRect->setIcon(QIcon(QString::fromUtf8("../undo/icons/rectangle.png")));
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
lineEditX = new QLineEdit(centralwidget);
lineEditX->setObjectName(QString::fromUtf8("lineEditX"));
lineEditX->setGeometry(QRect(80, 40, 51, 31));
widget = new QWidget(centralwidget);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(160, 10, 661, 501));
QPalette palette;
QBrush brush(QColor(255, 255, 255, 255));
brush.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Base, brush);
QBrush brush1(QColor(170, 255, 127, 255));
brush1.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Window, brush1);
palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
widget->setPalette(palette);
widget->setAutoFillBackground(true);
lineEditY = new QLineEdit(centralwidget);
lineEditY->setObjectName(QString::fromUtf8("lineEditY"));
lineEditY->setGeometry(QRect(80, 80, 51, 31));
lineEditHeight = new QLineEdit(centralwidget);
lineEditHeight->setObjectName(QString::fromUtf8("lineEditHeight"));
lineEditHeight->setGeometry(QRect(80, 160, 51, 31));
lineEditWidth = new QLineEdit(centralwidget);
lineEditWidth->setObjectName(QString::fromUtf8("lineEditWidth"));
lineEditWidth->setGeometry(QRect(80, 120, 51, 31));
labelX = new QLabel(centralwidget);
labelX->setObjectName(QString::fromUtf8("labelX"));
labelX->setGeometry(QRect(50, 50, 21, 18));
labelY = new QLabel(centralwidget);
labelY->setObjectName(QString::fromUtf8("labelY"));
labelY->setGeometry(QRect(50, 90, 17, 18));
labelHeight = new QLabel(centralwidget);
labelHeight->setObjectName(QString::fromUtf8("labelHeight"));
labelHeight->setGeometry(QRect(30, 170, 47, 20));
labelWidth = new QLabel(centralwidget);
labelWidth->setObjectName(QString::fromUtf8("labelWidth"));
labelWidth->setGeometry(QRect(30, 130, 41, 18));
MainWindow->setCentralWidget(centralwidget);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
toolBar = new QToolBar(MainWindow);
toolBar->setObjectName(QString::fromUtf8("toolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);

toolBar->addAction(actionRect);

retranslateUi(MainWindow);

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

void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionRect->setText(QApplication::translate("MainWindow", "Rect", 0, QApplication::UnicodeUTF8));
labelX->setText(QApplication::translate("MainWindow", "X", 0, QApplication::UnicodeUTF8));
labelY->setText(QApplication::translate("MainWindow", "Y", 0, QApplication::UnicodeUTF8));
labelHeight->setText(QApplication::translate("MainWindow", "Height", 0, QApplication::UnicodeUTF8));
labelWidth->setText(QApplication::translate("MainWindow", "Width", 0, QApplication::UnicodeUTF8));
toolBar->setWindowTitle(QApplication::translate("MainWindow", "toolBar", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui

#endif // UI_MAINWINDOW_H



#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
#include <QGridLayout>
class MainWindow: public QMainWindow, public Ui_MainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
QWidget *wd[100];
QGridLayout *layout;
private slots:
void newRect();

};
#endif // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"

int num;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
num=1;
layout = new QGridLayout();
connect(actionRect, SIGNAL(triggered()), this, SLOT(newRect()));

}

void MainWindow::newRect()
{
wd[num] = new QWidget(widget);

layout->addWidget(wd[num]);
widget->setLayout(layout);
wd[num]->show();
lineEditX->setText( QString::number(wd[num]->x()));
lineEditY->setText( QString::number(wd[num]->y()));
lineEditWidth->setText( QString::number(wd[num]->width()));
lineEditHeight->setText( QString::number(wd[num]->height()));
num++;

}


The code is as above.
It displays the coordinates in its QLineEdit; but not displays the run time QWidget wd[num].
What should I do for that?

jacek
17th December 2007, 12:11
widget->setLayout(layout);
Setting the layout more than once is asking for trouble. Move that line to the constructor.


but not displays the run time QWidget wd[num].
What should I do for that?
What do you expect to see? QWidgets are transparent. Try creating QPushButtons to see if your code works.