Results 1 to 6 of 6

Thread: Dynamic widget not displayed upon Qwidget

  1. #1
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question Dynamic widget not displayed upon Qwidget

    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
    Qt Code:
    1. public:
    2. QAction *actionRect;
    3. QWidget *centralwidget;
    4. QWidget *widget;
    5. QLineEdit *lineEdit;
    6. QPushButton *pushButton;
    7. QPushButton *pushButton_2;
    8. QStatusBar *statusbar;
    9. QToolBar *toolBar;
    To copy to clipboard, switch view to plain text mode 
    and in void setupUi(QMainWindow *MainWindow) code is following;
    Qt Code:
    1. centralwidget = new QWidget(MainWindow);
    2. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    3. widget = new QWidget(centralwidget);
    4. widget->setObjectName(QString::fromUtf8("widget"));
    5. widget->setGeometry(QRect(90, 0, 721, 561));
    To copy to clipboard, switch view to plain text mode 


    //mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QMainWindow>
    4. #include "ui_mainwindow.h"
    5.  
    6. class MainWindow: public QMainWindow, public Ui_MainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = 0);
    12. QWidget *wd[100];
    13.  
    14. private slots:
    15. void newRect();
    16.  
    17. };
    18. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    After that I am calling newRect slot which is connected with QAction *actionRect like this in MainWindow class constructor.
    //mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. int num;
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. setupUi(this);
    8. num=1;
    9.  
    10. connect(actionRect, SIGNAL(triggered()), this, SLOT(newRect()));
    11.  
    12. }
    13.  
    14. void MainWindow::newRect()
    15. {
    16. wd[num] = new QWidget(this->centralwidget);
    17. //wd[num]->setGeometry(5,50,30,30);
    18.  
    19. wd[num]->show();
    20. lineEdit->setText( QString::number(wd[num]->height()));
    21. num++;
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    My wd[num] widget is not displaying upon widget.
    What shuld I do for this?
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  2. #2
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic widget not displayed upon Qwidget

    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.

  3. #3
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic widget not displayed upon Qwidget

    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;
    Qt Code:
    1. void MainWindow::newRect()
    2. {
    3. wd[num] = new QWidget(widget);
    4. wd[num]->show();
    5. lineEdit->setText( QString::number(wd[num]->height()));
    6. num++;
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 
    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!
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic widget not displayed upon Qwidget

    Quote Originally Posted by ashukla View Post
    There is no widgets displays inside widget.
    What makes you think that these widgets aren't shown?

    Try:
    Qt Code:
    1. wd[num] = new QPushButton( "aaa", widget );
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by ashukla View Post
    If I am layouting the widget.
    At runtime message appears Layout is already set.
    How do you set that layout and on which widget?

  5. #5
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic widget not displayed upon Qwidget

    Dear Sir!
    Qt Code:
    1. #ifndef UI_MAINWINDOW_H
    2. #define UI_MAINWINDOW_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QLabel>
    9. #include <QtGui/QLineEdit>
    10. #include <QtGui/QMainWindow>
    11. #include <QtGui/QStatusBar>
    12. #include <QtGui/QToolBar>
    13. #include <QtGui/QWidget>
    14.  
    15. class Ui_MainWindow
    16. {
    17. public:
    18. QAction *actionRect;
    19. QWidget *centralwidget;
    20. QLineEdit *lineEditX;
    21. QWidget *widget;
    22. QLineEdit *lineEditY;
    23. QLineEdit *lineEditHeight;
    24. QLineEdit *lineEditWidth;
    25. QLabel *labelX;
    26. QLabel *labelY;
    27. QLabel *labelHeight;
    28. QLabel *labelWidth;
    29. QStatusBar *statusbar;
    30. QToolBar *toolBar;
    31.  
    32. void setupUi(QMainWindow *MainWindow)
    33. {
    34. if (MainWindow->objectName().isEmpty())
    35. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    36. MainWindow->resize(824, 600);
    37. actionRect = new QAction(MainWindow);
    38. actionRect->setObjectName(QString::fromUtf8("actionRect"));
    39. actionRect->setIcon(QIcon(QString::fromUtf8("../undo/icons/rectangle.png")));
    40. centralwidget = new QWidget(MainWindow);
    41. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    42. lineEditX = new QLineEdit(centralwidget);
    43. lineEditX->setObjectName(QString::fromUtf8("lineEditX"));
    44. lineEditX->setGeometry(QRect(80, 40, 51, 31));
    45. widget = new QWidget(centralwidget);
    46. widget->setObjectName(QString::fromUtf8("widget"));
    47. widget->setGeometry(QRect(160, 10, 661, 501));
    48. QPalette palette;
    49. QBrush brush(QColor(255, 255, 255, 255));
    50. brush.setStyle(Qt::SolidPattern);
    51. palette.setBrush(QPalette::Active, QPalette::Base, brush);
    52. QBrush brush1(QColor(170, 255, 127, 255));
    53. brush1.setStyle(Qt::SolidPattern);
    54. palette.setBrush(QPalette::Active, QPalette::Window, brush1);
    55. palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
    56. palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
    57. palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
    58. palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
    59. widget->setPalette(palette);
    60. widget->setAutoFillBackground(true);
    61. lineEditY = new QLineEdit(centralwidget);
    62. lineEditY->setObjectName(QString::fromUtf8("lineEditY"));
    63. lineEditY->setGeometry(QRect(80, 80, 51, 31));
    64. lineEditHeight = new QLineEdit(centralwidget);
    65. lineEditHeight->setObjectName(QString::fromUtf8("lineEditHeight"));
    66. lineEditHeight->setGeometry(QRect(80, 160, 51, 31));
    67. lineEditWidth = new QLineEdit(centralwidget);
    68. lineEditWidth->setObjectName(QString::fromUtf8("lineEditWidth"));
    69. lineEditWidth->setGeometry(QRect(80, 120, 51, 31));
    70. labelX = new QLabel(centralwidget);
    71. labelX->setObjectName(QString::fromUtf8("labelX"));
    72. labelX->setGeometry(QRect(50, 50, 21, 18));
    73. labelY = new QLabel(centralwidget);
    74. labelY->setObjectName(QString::fromUtf8("labelY"));
    75. labelY->setGeometry(QRect(50, 90, 17, 18));
    76. labelHeight = new QLabel(centralwidget);
    77. labelHeight->setObjectName(QString::fromUtf8("labelHeight"));
    78. labelHeight->setGeometry(QRect(30, 170, 47, 20));
    79. labelWidth = new QLabel(centralwidget);
    80. labelWidth->setObjectName(QString::fromUtf8("labelWidth"));
    81. labelWidth->setGeometry(QRect(30, 130, 41, 18));
    82. MainWindow->setCentralWidget(centralwidget);
    83. statusbar = new QStatusBar(MainWindow);
    84. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    85. MainWindow->setStatusBar(statusbar);
    86. toolBar = new QToolBar(MainWindow);
    87. toolBar->setObjectName(QString::fromUtf8("toolBar"));
    88. MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);
    89.  
    90. toolBar->addAction(actionRect);
    91.  
    92. retranslateUi(MainWindow);
    93.  
    94. QMetaObject::connectSlotsByName(MainWindow);
    95. } // setupUi
    96.  
    97. void retranslateUi(QMainWindow *MainWindow)
    98. {
    99. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    100. actionRect->setText(QApplication::translate("MainWindow", "Rect", 0, QApplication::UnicodeUTF8));
    101. labelX->setText(QApplication::translate("MainWindow", "X", 0, QApplication::UnicodeUTF8));
    102. labelY->setText(QApplication::translate("MainWindow", "Y", 0, QApplication::UnicodeUTF8));
    103. labelHeight->setText(QApplication::translate("MainWindow", "Height", 0, QApplication::UnicodeUTF8));
    104. labelWidth->setText(QApplication::translate("MainWindow", "Width", 0, QApplication::UnicodeUTF8));
    105. toolBar->setWindowTitle(QApplication::translate("MainWindow", "toolBar", 0, QApplication::UnicodeUTF8));
    106. } // retranslateUi
    107.  
    108. };
    109.  
    110. namespace Ui {
    111. class MainWindow: public Ui_MainWindow {};
    112. } // namespace Ui
    113.  
    114. #endif // UI_MAINWINDOW_H
    115.  
    116.  
    117.  
    118. #ifndef MAINWINDOW_H
    119. #define MAINWINDOW_H
    120. #include <QMainWindow>
    121. #include "ui_mainwindow.h"
    122. #include <QGridLayout>
    123. class MainWindow: public QMainWindow, public Ui_MainWindow
    124. {
    125. Q_OBJECT
    126.  
    127. public:
    128. MainWindow(QWidget *parent = 0);
    129. QWidget *wd[100];
    130. QGridLayout *layout;
    131. private slots:
    132. void newRect();
    133.  
    134. };
    135. #endif // MAINWINDOW_H
    136.  
    137. //mainwindow.cpp
    138. #include "mainwindow.h"
    139.  
    140. int num;
    141. MainWindow::MainWindow(QWidget *parent)
    142. : QMainWindow(parent)
    143. {
    144. setupUi(this);
    145. num=1;
    146. layout = new QGridLayout();
    147. connect(actionRect, SIGNAL(triggered()), this, SLOT(newRect()));
    148.  
    149. }
    150.  
    151. void MainWindow::newRect()
    152. {
    153. wd[num] = new QWidget(widget);
    154.  
    155. layout->addWidget(wd[num]);
    156. widget->setLayout(layout);
    157. wd[num]->show();
    158. lineEditX->setText( QString::number(wd[num]->x()));
    159. lineEditY->setText( QString::number(wd[num]->y()));
    160. lineEditWidth->setText( QString::number(wd[num]->width()));
    161. lineEditHeight->setText( QString::number(wd[num]->height()));
    162. num++;
    163.  
    164. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic widget not displayed upon Qwidget

    Quote Originally Posted by ashukla View Post
    widget->setLayout(layout);
    Setting the layout more than once is asking for trouble. Move that line to the constructor.

    Quote Originally Posted by ashukla View Post
    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.

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2007, 18:42
  2. get custom widget to inherit qwidget
    By CopyrightPhilly in forum Qt Tools
    Replies: 3
    Last Post: 4th October 2007, 08:58
  3. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 12:07
  4. Dynamic updates to a table widget
    By guiGuy in forum Qt Programming
    Replies: 6
    Last Post: 1st June 2006, 20:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.