.ui文件

2018-08-06  本文已影响17人  downdemo
无法打开文档 对应的ui_xxx.h文件 新建ui文件 选择编译 编译成功 编译生成的ui_test.h Generated Files下无新生成的ui_test.h 手动添加 选择头文件 双击ui_dlg.h 移除无法打开的文件后
// #include <QVBoxLayout>
// #include <QMenuBar>

private:
    Ui::dlg *ui;
    // 以下为添加控件,记住添加对应的头文件
    QVBoxLayout* layout;
    QMenuBar* menuBar;
    QAction* actionNew;
ui->setupUi(this);
// 以下为添加的实现
layout = new QVBoxLayout(this);
menuBar = new QMenuBar(this);
menuBar->setGeometry(QRect(0, 0, 400, 24));
QMenu* menu = menuBar->addMenu(tr("&File"));
actionNew = menu->addAction(tr("&New"));
actionNew->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
layout->setMenuBar(menuBar);
setLayout(layout);

例子

// file "test.h"

#ifndef TEST_H
#define TEST_H

#include <QMainWindow>

namespace Ui {
    class test;
}

class test : public QMainWindow
{
    Q_OBJECT

public:
    explicit test(QWidget *parent = 0);
    ~test();

private:
    Ui::test *ui;
};

#endif // TEST_H
#include "test.h"
#include "ui_Widget.h"

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

test::~test()
{
    delete ui;
}
// file "main.cpp"
#include "QtGuiApplication2.h"
#include "test.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //QtGuiApplication2 w;
    test w;
    w.show();
    return a.exec();
}
#ifndef TEST_H
#define TEST_H

#include <QMainWindow>
// 以下为新增的头文件
#include <QVBoxLayout>
#include <QMenuBar>

namespace Ui {
    class test;
}

class test : public QMainWindow
{
    Q_OBJECT

public:
    explicit test(QWidget *parent = 0);
    ~test();

private:
    Ui::test *ui;
    // 以下为新增的成员
    QVBoxLayout* layout;
    QMenuBar* menuBar;
    QAction* actionNew;
};

#endif // TEST_H
#include "test.h"
#include "ui_Widget.h"

test::test(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::test)
{
    ui->setupUi(this);
    // 以下为新增的实现
    layout = new QVBoxLayout(this);
    menuBar = new QMenuBar(this);
    menuBar->setGeometry(QRect(0, 0, 400, 24));
    QMenu* menu = menuBar->addMenu(tr("&File"));
    actionNew = menu->addAction(tr("&New"));
    actionNew->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
    layout->setMenuBar(menuBar);
    setLayout(layout);
}

test::~test()
{
    delete ui;
}
上一篇 下一篇

猜你喜欢

热点阅读