我正在尋找一種簡單的方式將手動編碼設計鏈接到Qt中的小部件應用程序的用戶界面。我打算使用UI構建器來輕鬆調整佈局並獲得適當的間距,這在沒有UI構建器的情況下很難做到。
我要創建我打算使用QVector< QVector<QPushButton*> >
按鈕的3×3格(我不知道我怎麼會在UI建設者做到這一點。)Qt:如何將手動編寫的UI與UI生成器相鏈接?
以下是我已經試過了,爲什麼不顯示的按鈕即使我將每個按鈕的父級設置爲小部件?
的main.cpp
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
在window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QPushButton>
namespace Ui {
class Window;
}
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
~Window();
private:
Ui::Window *ui;
static const int tiles = 50, height = 600, width = 500;
QVector< QVector<QPushButton*> > cells;
};
#endif // WINDOW_H
window.cpp
#include "window.h"
#include "ui_window.h"
Window::Window(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window)
{
ui->setupUi(this);
this->resize(width, height);
int j = 0;
for(auto &row : cells)
{
int i = 0;
for(auto &col : row)
{
col = new QPushButton(this);
col->setGeometry(i, j, tiles, tiles);
i += tiles;
}
j += tiles;
}
}
Window::~Window()
{
delete ui;
for(auto &row : cells)
{
for(auto &col : row)
{
delete col;
}
}
}
任何幫助將是apprec iated。