设置标题栏图标,位置与大小示例
#include<QApplication>
#include<QWidget>
#include<QDebug>
#include"mywidget.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
//功能新的需求:不再使用框架提供的窗口,自定义一个窗口;
//派生类;
MyWidget w; //MyWidget类继承于基类QWidget类;
w.show();
return app.exec();
}
//------------ MyWidget.h -------------------
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include<QMouseEvent>
#include <QWidget>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = );
void mousePressEvent(QMouseEvent *ev);
void mouseMoveEvent(QMouseEvent *ev);
//移动窗口
void mouseReleaseEvent(QMouseEvent *ev);
//释放窗口
//鼠标原始位置
QPoint posMouseOrigin;
signals:
public slots:
};
#endif // MYWIDGET_H
//------------ MyWidget.cpp -------------------
#include "mywidget.h"
#include<QDebug>
#include<QWidget>
#include<QPoint>
#include<QMouseEvent>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
this->setWindowTitle("QQ ");
this->setWindowIcon(QIcon(":/images/po.jpg"));//设置图标
this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
this->setGeometry(QRect(, , , ));//可设置窗口显示的方位与大小this->setWindowFlags(Qt::FramelessWindowHint); //去掉标题栏; }
//获取鼠标的按下操作
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
this->posMouseOrigin = QCursor::pos(); //cursor是一个光标类;
}
//获得鼠标移动的操作
void MyWidget::mouseMoveEvent(QMouseEvent *ev)
{
QPoint ptMouseNow = QCursor::pos();
QPoint ptDelta = ptMouseNow - this->posMouseOrigin;
move(this->pos() + ptDelta); posMouseOrigin = ptMouseNow;
}
//获得鼠标的释放操作;
void MyWidget::mouseReleaseEvent(QMouseEvent *ev)
{
}