QT win32创建窗口

醉酒当歌 提交于 2019-11-29 04:55:16

1. win32主窗口修改参数 及增加窗口类的头文件(mainClass.h)

#include <QtWidgets/QApplication>
//#include <QtCore/QCoreApplication>
#include "mainClass.h"


int main(int argc, char *argv[])
{
	//QCoreApplication a(argc, argv);
	QApplication a(argc, argv);

   QFont font("ZYSong18030", 12);
	a.setFont(font);

	mainClass *myClass = new mainClass(0);
	myClass->initDlg();


	return a.exec();
}

2. 创建QT类:

1. 头文件

#pragma once

#include <QObject>
#include "QtWidgets/qmainwindow.h"
#include "QtWidgets/qsplitter.h"
#include "QtCore/qobject.h"
#include "QtWidgets/qlabel.h"
#include "QtWidgets/qtextedit.h"
#include "QtWidgets/qpushbutton.h"
#include "QtCore/qdebug.h"


#include "opencvhead.h"
#include "hikvisonMyCamera.h"
#include "myTcpServer.h"


#ifdef _DEBUG
#pragma comment(lib, "Qt5Networkd.lib")
#pragma comment(lib, "Qt5Cored.lib")
#pragma comment(lib, "Qt5Guid.lib")
#pragma comment(lib, "Qt5Widgetsd.lib")
#else
#pragma comment(lib, "Qt5Network.lib")
#pragma comment(lib, "Qt5Core.lib")
#pragma comment(lib, "Qt5Gui.lib")
#pragma comment(lib, "Qt5Widgets.lib")
#endif


class mainClass : public QObject
{
	Q_OBJECT

public:
	mainClass(QObject *parent);
	~mainClass();
public:
	void initDlg();
	void childDlg();
	void LogTxt(QString szMsg);
	Mat getImg();
private slots:
	void openCam();
	void run();
	void manual();


private:
	ChikvisionMyCamera *myCamera;
	int nOpen;
	int nIndex;
#pragma region [dlg]
private:
	//主界面
	QSplitter *splitterMain;

	//被左右分隔 
	QSplitter *splitterLeft;

	//操作界面对话框
	QWidget *widget;

	//相机显示
	QLabel *Cam;

	//日志显示控件
	QTextEdit *logDlg;

	//按钮
	QPushButton *camBtn;
	QPushButton *runBtn;
	QPushButton *manualBtn;

#pragma endregion [dlg]

};





2. class内容
#include "mainClass.h"

void mainClass::initDlg()
{
	splitterMain = new QSplitter(Qt::Horizontal);
	splitterMain->setGeometry(150, 150, 1200, 800);

	splitterLeft = new QSplitter(Qt::Horizontal, splitterMain);//::Vertical
	splitterMain->addWidget(splitterLeft);


	widget = new QWidget;
	widget->setWindowFlags(Qt::Window);
	splitterMain->addWidget(widget);

	Cam = new QLabel;
	Cam->setFrameShape(QFrame::Box);
	splitterLeft->addWidget(Cam);


	//分离器被设置为2:1
	QList<int> q = { 20,10 };
	splitterMain->setSizes(q);


	//设置边界颜色
	splitterMain->setStyleSheet("QSplitter::handle {background-color:blue}");

	//固定分离器分割线
	QSplitterHandle *splitterMainHandle = splitterMain->handle(1);
	if (splitterMainHandle)
		splitterMainHandle->setDisabled(true);

	this->setParent(splitterMain);
	splitterMain->setWindowTitle(QObject::tr("main"));
	splitterMain->show();

	childDlg();
}

void mainClass::childDlg()
{
	camBtn = new QPushButton(QObject::tr("打开相机"), widget);
	camBtn->setGeometry(10, 10, 80, 40);
	camBtn->show();

	runBtn = new QPushButton(QObject::tr("自动"), widget);
	runBtn->setGeometry(10, 340, 80, 40);
	runBtn->show();

	manualBtn = new QPushButton(QObject::tr("手动"), widget);
	manualBtn->setGeometry(120, 340, 80, 40);
	manualBtn->show();

	//日志显示位置
	logDlg = new QTextEdit(widget);
	logDlg->setGeometry(0, 400, 400, 400);
	logDlg->show();

	//btn连接
	connect(camBtn,    SIGNAL(clicked()), this,   SLOT(openCam()) );
	connect(runBtn,    SIGNAL(clicked()), this,   SLOT(run())     );
	connect(manualBtn, SIGNAL(clicked()), this,   SLOT(manual())  );
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!