設置標題欄圖標,位置與大小示例
#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 = 0); 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(950, 55, 350, 250));//可設置窗口顯示的方位與大小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) { }