代码如下:
untitled.ui 用QT Designer做界面生成代码文件*.ui
Quote:
Form
0
0
399
312
Form
8
6
0
6
Insert
Qt::Horizontal
121
20
Close
10
10
411
291
9
9
401
297
pushButton_3
clicked()
Form
close()
349
291
314
311
ui_untitled.h 再用命令:uic ui_untitled.ui> ui_untitled.h 生成*.h文件
Quote:
#ifndef UI_UNTITLED_H
#define UI_UNTITLED_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class Ui_Form
{
public:
QWidget *widget;
QWidget *widget1;
QGridLayout *gridLayout;
QListWidget *listWidget;
QHBoxLayout *hboxLayout;
QPushButton *pushButton;
QSpacerItem *spacerItem;
QPushButton *pushButton_3;
QLineEdit *lineEdit;
void setupUi(QWidget *Form)
{
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(QSize(399, 312).expandedTo(Form->minimumSizeHint()));
widget = new QWidget(Form);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(10, 10, 411, 291));
widget1 = new QWidget(Form);
widget1->setObjectName(QString::fromUtf8("widget1"));
widget1->setGeometry(QRect(9, 9, 401, 297));
gridLayout = new QGridLayout(Form);
gridLayout->setSpacing(6);
gridLayout->setMargin(8);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
listWidget = new QListWidget(Form);
listWidget->setObjectName(QString::fromUtf8("listWidget"));
gridLayout->addWidget(listWidget, 0, 0, 1, 1);
hboxLayout = new QHBoxLayout();
hboxLayout->setSpacing(6);
hboxLayout->setMargin(0);
hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
pushButton = new QPushButton(Form);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
hboxLayout->addWidget(pushButton);
spacerItem = new QSpacerItem(121, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
hboxLayout->addItem(spacerItem);
pushButton_3 = new QPushButton(Form);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
hboxLayout->addWidget(pushButton_3);
gridLayout->addLayout(hboxLayout, 2, 0, 1, 1);
lineEdit = new QLineEdit(Form);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
gridLayout->addWidget(lineEdit, 1, 0, 1, 1);
retranslateUi(Form);
QObject::connect(pushButton_3, SIGNAL(clicked()), Form, SLOT(close()));
QMetaObject::connectSlotsByName(Form);
} // setupUi
void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form"));
pushButton->setText(QApplication::translate("Form", "Insert"));
pushButton_3->setText(QApplication::translate("Form", "Close"));
Q_UNUSED(Form);
} // retranslateUi
};
namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui
#endif // UI_UNTITLED_H
main.cpp 自写一个main.cpp程序工程文件
Quote:
#include
#include "dlg.h"//加载*.h文件
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyDlg *mydlg= new MyDlg;//建立类
return mydlg->exec();
}
dlg.h 写一个dlg类的类文件
Quote:
#ifndef DLG_H
#define DLG_H
#include //引用类库
#include "ui_untitled.h" //引用界面头文件
class MyDlg: public Qdialog //定义自己的类库
{
Q_OBJECT
public:
MyDlg(); //构建函数
private slots:
void InsertSlot(); //定义Slot
private:
Ui::Form ui; //定义ui命名空间 form类 ui
};
#endif
dlg.cpp 写一个dlg的程序文件
Quote:
#include "dlg.h" //引用定义类头文件
#include
#include
MyDlg::MyDlg() //构建类本身
{
ui.setupUi(this); //界面
QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(InsertSlot())); //建事件联接
}
void MyDlg::InsertSlot() //事件函数
{
QString strInsert = ui.lineEdit->text();
if (strInsert.isEmpty())
{
QMessageBox::warning(this, tr("Alert"),
tr("You have not input any character"),
tr("OK"));
ui.lineEdit->setFocus();
}
else
{
ui.listWidget->addItem(strInsert);
ui.lineEdit->clear();
}
}
原始文件为:untitled.ui, main.cpp, dlg.h, dlg.cpp
运行下面命令:编译
Quote:
qmake -project
qmake
make
阅读(1144) | 评论(0) | 转发(0) |