main.cpp
#include "myqt.h"
#include <QtWidgets/QApplication>
#include <QtWidgets>
#include "XWidget.h"
#include "XThread.h"
using namespace Qt;
//QWidget* w = NULL; //QWidget声明
XWidget* w = NULL;
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //进行信号与槽的处理,放到QWight前面
w = new XWidget();
//XThread xt;
//xt.start(); //开启线程
//w->setWindowTitle("my widget"); //设置窗体标题
//w->show(); //显示窗体
// w->setGeometry(200, 100, 800, 600); //(起点x,起点y,宽,高)
//w->setGeometry(200, 100, 800, 600); //(起点x,起点y,宽,高)
//QObject::connect(&xt, SIGNAL(Move(int, int)), w, SLOT(move(int, int)));
//w->setWindowState(Qt::WindowNoState);//设置窗口状态
/*
enum WindowState {
WindowNoState = 0x00000000,
WindowMinimized = 0x00000001,
WindowMaximized = 0x00000002,
WindowFullScreen = 0x00000004,
WindowActive = 0x00000008
};
*/
//w->setWindowFlags(Qt::FramelessWindowHint); //关闭标题栏和边框
//关闭最小窗口按钮和最大窗口控件
//w->setWindowFlag(WindowMinimizeButtonHint,false); //单独对某一位设置
//w->setWindowFlag(WindowMaximizeButtonHint,false);
//标题栏保留,关闭所有按钮
//w->setWindowFlags(WindowTitleHint|CustomizeWindowHint);
//只保留关闭按钮
w->setWindowFlags (WindowCloseButtonHint); //对各个为置为1
w->show();
/* myqt w;
w.show();*/
return a.exec();
}
XWidget.h
#pragma once
#include <qwidget.h>
class XWidget : public QWidget
{
Q_OBJECT //宏,表示支持信号和槽
public:
XWidget();
virtual ~XWidget();
signals:
void Hide();
public slots:
void move(int x, int y) { QWidget::move(x, y); }
};
XWidget.cpp
#include "XWidget.h"
XWidget::XWidget()
{
connect(this, SIGNAL(Hide()), this, SLOT(hide()));
}
XWidget::~XWidget()
{
}
XThread.h
#pragma once
#include <qthread.h>
class XThread : public QThread
{
Q_OBJECT
public:
void run() //重写run()函数
{
//这里要使用信号,槽函数不要在线程中调用
//msleep(3000);
//w->Hide();
for (int x = 0; x < 1000; x++)
{
Move(x,x);
msleep(50);
}
}
XThread();
~XThread();
signals:
void Move(int x, int y);
};
XThread.cpp
#include "XThread.h"
XThread::XThread()
{
}
XThread::~XThread()
{
terminate(); //终止线程
}