PDA

View Full Version : Understanding the .ui -> code connection in QT Creator



lucasvickers
22nd February 2010, 17:23
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


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

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

* this is what I want to do eventually

int numberDatasets = 5;
for(int i=0; i<numberDatasets; ++i) {
int row = datasetMain_table->rowCount();
datasetMain_table->insertRow(row);
// name, description, date, notes
datasetMain_table->setItem(row, 0, "dataset name blah.dat");
datasetMain_table->setItem(row, 1, "This is a test dataset from 1/1/1/1/15953 hb blah");
datasetMain_table->setItem(row, 2, "6/3/86");
datasetMain_table->setItem(row, 3, "We have this run on 1,2,3");
}*/
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

Lesiok
22nd February 2010, 18:21
Class Ui::MainWindow has public pointers to all elements of Yours ui. Just look into ui_mainwindow.h.

lucasvickers
22nd February 2010, 20:08
got it!

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

Lykurg
22nd February 2010, 22:11
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.