Results 1 to 4 of 4

Thread: Understanding the .ui -> code connection in QT Creator

  1. #1
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Understanding the .ui -> code connection in QT Creator

    Hello,

    I am using QT Creator. I added a QTableWidget using the .ui editor. When I run the project the widget shows up.
    I now want to programmatically add a few rows to the QTableWidget.
    I am confused because in the mainwindow.cpp/.h I see no reference to my QTableWidget.

    1) How is the QTableWidget actually showing up in the UI if it is not even mentioned in the mainwindow.cpp/.h?
    2) Is there a way to get the mainwindow.cpp/.h to synch up with the .ui file?

    thanks!
    code below

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow {
    11. Q_OBJECT
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15.  
    16. protected:
    17. void changeEvent(QEvent *e);
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. };
    22.  
    23. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. /*
    10.  
    11.   * this is what I want to do eventually
    12.  
    13.   int numberDatasets = 5;
    14.   for(int i=0; i<numberDatasets; ++i) {
    15.   int row = datasetMain_table->rowCount();
    16.   datasetMain_table->insertRow(row);
    17.   // name, description, date, notes
    18.   datasetMain_table->setItem(row, 0, "dataset name blah.dat");
    19.   datasetMain_table->setItem(row, 1, "This is a test dataset from 1/1/1/1/15953 hb blah");
    20.   datasetMain_table->setItem(row, 2, "6/3/86");
    21.   datasetMain_table->setItem(row, 3, "We have this run on 1,2,3");
    22.   }*/
    23. }
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28. }
    29.  
    30. void MainWindow::changeEvent(QEvent *e)
    31. {
    32. QMainWindow::changeEvent(e);
    33. switch (e->type()) {
    34. case QEvent::LanguageChange:
    35. ui->retranslateUi(this);
    36. break;
    37. default:
    38. break;
    39. }
    To copy to clipboard, switch view to plain text mode 
    }

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Understanding the .ui -> code connection in QT Creator

    Class Ui::MainWindow has public pointers to all elements of Yours ui. Just look into ui_mainwindow.h.

  3. #3
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Understanding the .ui -> code connection in QT Creator

    got it!

    thanks. not sure why they don't show this file in the create ui.

  4. #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: Understanding the .ui -> code connection in QT Creator

    Quote Originally Posted by lucasvickers View Post
    not sure why they don't show this file in the create ui.
    It's because you don't have to use it. And really, it's better to hide it, because otherwise people going to change things inside the header file (even if there is a warning) and then complaining when changes are getting lost...
    You only have to take care about your ui file where you can see the names, with which you can access the objects. More you don't need. If you need the ui_*.h then you have to code your GUI by hand.

Similar Threads

  1. Replies: 21
    Last Post: 14th December 2009, 08:11
  2. Replies: 2
    Last Post: 28th August 2009, 07:12
  3. I need help understanding QGraphicsView
    By aarelovich in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 20:02
  4. Replies: 0
    Last Post: 8th July 2009, 08:12
  5. How to turn off translator code in creator
    By sanfordpaul in forum Newbie
    Replies: 0
    Last Post: 4th March 2009, 23:39

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.