April 22, 2012

zeller zeller
Lab Rat
7 posts

[SOLVED] Add widgets manually to Qt designer generated code

 

Hello,

First: I stated using qt yesterday so be easy on me please.
I created a simple layout with designer and now I want to add new widgets to it manually, but as I see it looks impossible, since there is no “redraw” or such. Is there any way to do it, apart from copying the code from setupUi and discarding the gui design?

Here is what I’m doing:

  1.     ui->setupUi(this);
  2.  
  3.     MapArea wtf;
  4.     QScrollArea *sa = new QScrollArea();
  5.     sa->setWidget(&wtf);
  6.     QVBoxLayout *bl = new QVBoxLayout(ui->widget);
  7.     bl->addWidget(sa);
  8.     QTextEdit *te = new QTextEdit();
  9.     bl->addWidget(te);
  10.     ui->widget->setLayout(bl);
  11.     //and now something here to redraw the window with the new widgets

thanks
david

3 replies

April 22, 2012

Sam Sam
Area 51 Engineer
609 posts

Hi,

Since you have already created your QMainWindow in the designer you can write the following code in your constructor:

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5.     ui->setupUi(this);
  6.     QLabel *lblFirstName = new QLabel("First Name");
  7.     QLabel *lblLastName = new QLabel("Last Name");
  8.  
  9.     QLineEdit *txtFirstName = new QLineEdit();
  10.     QLineEdit *txtLastName = new QLineEdit();
  11.  
  12.     QPushButton *btnOk = new QPushButton("OK");
  13.     QPushButton *btnCancel = new QPushButton("Cancel");
  14.  
  15.     QHBoxLayout *top = new QHBoxLayout;
  16.     QHBoxLayout *bottom = new QHBoxLayout;
  17.     QHBoxLayout *btnLayout = new QHBoxLayout;
  18.  
  19.     top->addWidget(lblFirstName);
  20.     top->addWidget(txtFirstName);
  21.  
  22.     bottom->addWidget(lblLastName);
  23.     bottom->addWidget(txtLastName);
  24.  
  25.     btnLayout->addStretch();
  26.     btnLayout->addWidget(btnOk);
  27.     btnLayout->addWidget(btnCancel);
  28.  
  29.  
  30.     QVBoxLayout *mainLayout = new QVBoxLayout(this);
  31.     mainLayout->addLayout(top);
  32.     mainLayout->addLayout(bottom);
  33.     mainLayout->addLayout(btnLayout);
  34.  
  35.     this->centralWidget()->setLayout(mainLayout);
  36.  
  37. }

This is just an example to add widgets programatically. It shows user information with some buttons. You can start with the basics provided on internet and also Books [qt-project.org] on Qt.

April 22, 2012

zeller zeller
Lab Rat
7 posts

Thanks, my bad… (cause I’ve just did the same thing you wrote). The actual problem was that I already set a layout for the ‘widget’ component and I tried to add a new one in the code above, thus nothing happened. Now with this, it works (compare to the original code snippet):

  1.     ui->setupUi(this);
  2.  
  3.     MapArea wtf;
  4.     QScrollArea *sa = new QScrollArea();
  5.     sa->setWidget(&wtf);
  6.     QTextEdit *te = new QTextEdit();
  7.     ui->widget->layout()->addWidget(sa);
  8.     ui->widget->layout()->addWidget(te);

April 24, 2012

Sam Sam
Area 51 Engineer
609 posts

You are welcome !!!!

 
  ‹‹ [Solved] How do i create a static LIB      QtCreator’s SSH connection should accept ver 1.99 because it is compatible with 2.0 ››

You must log in to post a reply. Not a member yet? Register here!