Results 1 to 4 of 4

Thread: Adding QPushButton to *.ui file by hand

  1. #1
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    9
    Platforms
    Unix/X11 Windows

    Default Adding QPushButton to *.ui file by hand

    Hello!

    I've made a new QMainWindow project by QtCreator.
    Constructor looks like that:

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    }

    Is it possible to add for example QPushButton to the window generated form *.ui file just by writing it in constructor?

    I've tried to write:

    QPushButton button("button",ui->centralWidget);
    button.move(10,10);
    button.show();


    QPushButton button("button",this);
    button.move(10,10);
    button.show();

    but the button don't want to show.

    Please help
    s410i

  2. #2
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding QPushButton to *.ui file by hand

    You have to add the widget, assuming your centralWidget has a layout, this will add it to the last row:

    Qt Code:
    1. QPushButton button = new QPushButton;
    2. int rows = ui->centralWidget->rowCount();
    3. ui->centralWidget->addWidget(button rows, 0, rows, 0, 0);
    To copy to clipboard, switch view to plain text mode 

    edit: centralWidget HAS to have a layout assigned to it first, then:
    Qt Code:
    1. ui->centralWidget->layout->addWidget(button rows, 0, rows, 0, 0);
    To copy to clipboard, switch view to plain text mode 
    Last edited by vieraci; 23rd June 2009 at 01:23. Reason: updated contents

  3. The following user says thank you to vieraci for this useful post:

    s410i (23rd June 2009)

  4. #3
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    9
    Platforms
    Unix/X11 Windows

    Default Re: Adding QPushButton to *.ui file by hand

    I've tried just as you sad:

    I've added QGridLayout to *.ui file and:

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QPushButton button("Click");

    int rows = ui->gridLayout->rowCount();
    int cols = ui->gridLayout->columnCount();

    ui->gridLayout->addWidget(&button,rows,cols,0);
    }

    but when I compile the program there in no button.

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Adding QPushButton to *.ui file by hand

    of course, because your button is deleted after the ctor! create it on the heap, not on the stack!

  6. The following user says thank you to Lykurg for this useful post:

    s410i (23rd June 2009)

Similar Threads

  1. Adding Image to QPushButton
    By Seema Rao in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2011, 09:06
  2. Adding a new file to moc process in XCode
    By redneon in forum Qt Programming
    Replies: 0
    Last Post: 2nd May 2009, 18:49
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Adding property editor file browser popup...
    By thawkins in forum Qt Tools
    Replies: 1
    Last Post: 25th April 2007, 23:03

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.