如何使用ui文件制作一个简单的小部件? [英] how to use ui file for making a simple widget?

查看:51
本文介绍了如何使用ui文件制作一个简单的小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在qt中有一个带有退出按钮的简单窗口.工作代码如下所示

i have a simple window with a quit button in qt.The working code is shown below

 #include <QApplication>
 #include <QDialog>
 #include <QPushButton>

class MyWidget : public QWidget
{
public:
    MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(200, 120);

    QPushButton *btquit = new QPushButton(tr("Quit"), this);
    btquit->setGeometry(62, 40, 75, 30);
    btquit->setFont(QFont("Times", 18, QFont::Bold));

    connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    return app.exec();
}

现在我想使用 qt 设计器编写这个程序.我使用 qt 设计器在 ui 文件中创建了一个名为mywindow"的小部件和一个名为btquit"的主小部件内的按钮.如何用ui文件重写上面的代码.ui文件的名字是mywindow.ui

Now i want to code this program using qt designer.I created a widget named "mywindow" and a button inside that main widget named "btquit" in the ui file using qt designer. How to rewrite the above code with the ui file.The name of ui file is mywindow.ui

推荐答案

#include <QApplication>
#include <QDialog>
#include <QPushButton>
#include "ui_mywindow1.h"

class MyWidget : public QWidget,private Ui::mywindow
{
public:
    MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    setupUi(this);


    connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    return app.exec();
}

这篇关于如何使用ui文件制作一个简单的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆