⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qt系列之三:windows窗体.txt

📁 该资料是介绍Qt技术的一些心得体会
💻 TXT
字号:
在做设计Windows窗体的程序时候,我感触良多:编程能力固然重要,逻辑思维也很重要。
我们往往在为写出一个程序而高兴,但是实际上我们学到的只是皮毛,很重要的一点就是能很好的设计程序,包括设计类、类和类的联系、事件、消息响应等等。这就要求我们多想想设计的事情,最好能用UML给画出来,而不仅限于coding。没人想一辈子只当一个coder!
设计并实现一个简单窗体程序的步骤如下:
一、新建一个类,该类应该继承诸如QMainWindow、QWidget等类。
在此例中,我们设计一个名为Window的类,继承与QMainWindow类。如下:
#ifndef WINDOW_H
#define WINDOW_H

#include <QtGui/QMainWindow>
#include "QPushButton"
class Window : public QMainWindow
{
    Q_OBJECT

public:
    Window(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~Window();

private slots:
	void mainViewer();
	void aboutRSImage();
signals:
	void closeAll();
private:
	QPushButton *pMainViewer;
	QPushButton *pAblot;
	void Initialize();
	void setUpButton();
	void Dispose();
protected:
};

#endif // WINDOW_H

二、实现window.cpp文件
#include "window.h"
#include "QMessageBox"
Window::Window(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
this->Initialize();
}

Window::~Window()
{

}
void Window::Initialize()
{
this->setUpButton();
this->setGeometry(0,25,610,45);
};
void Window::Dispose()
{

}
void Window::setUpButton()
{
	this->pMainViewer=new QPushButton(QString::fromLocal8Bit("基本操作"),this);
	this->pMainViewer->setGeometry(0,2,100,40);
	this->pMainViewer->setDefault(true);
	connect(this->pMainViewer,SIGNAL(clicked()),this,SLOT(mainViewer()));
}
void Window::mainViewer()
{
QMessageBox::about(this,"Coding by RS","This is my first Qt window program");
}
void Window::aboutRSImage()
{
	QMessageBox::about(this,"Coding by RS","This is my first Qt window program");
}

三、完善main.cpp文件
#include <QtGui/QApplication>
#include "window.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window w;
	w.setWindowTitle(QString::fromLocal8Bit("武汉大学遥感信息工程学院"));
    w.show();
    a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    return a.exec();
}

当然,这个例子是一个非常简单的程序。如果要做比较复杂的程序,则必须要认真设计各个类,及其关系。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -