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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
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.
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.
Re: Understanding the .ui -> code connection in QT Creator
Quote:
Originally Posted by
lucasvickers
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.