新聞中心
使用Qt框架怎么實(shí)現(xiàn)一個(gè)透明無邊框窗口?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)專注于深州企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都商城網(wǎng)站開發(fā)。深州網(wǎng)站建設(shè)公司,為深州等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
第一步:開啟窗口的透明層。
setWindowFlags(Qt::FramelessWindowHint); /* 注意:如果單純開啟窗口透明層效果,在Windows系統(tǒng)中必須設(shè)置, 其他系統(tǒng)可忽略。 */ setAttribute(Qt::WA_TranslucentBackground);
第二步: 重寫paintEvent事件并使用QPainter畫透明層。
void paintEvent(QPaintEvent *) { QPainter painter(this); /* 0x20為透明層顏色,可自定義設(shè)置為0x0到0xff */ painter.fillRect(this->rect(), QColor(0, 0, 0, 0x20)); }
0x01 如何無邊框窗口?
設(shè)置setWindowFlags(Qt::FramelessWindowHint);
即可無邊框窗口,但無法移動和改變大小。
0x02 如何拖拽窗口?
由于系統(tǒng)窗口被設(shè)置為Qt::FramelessWindowHint
會導(dǎo)致窗口不能被拖動。通過捕獲鼠標(biāo)移動事件從而實(shí)現(xiàn)窗口移動。
void mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { /* 捕獲按下時(shí)坐標(biāo) */ m_startPoint = frameGeometry().topLeft() - event->globalPos(); } } void mouseMoveEvent(QMouseEvent *event) { /* 移動窗口 */ this->move(event->globalPos() + m_startPoint); }
0x03 完整代碼
#include#include #include #include #include class TransparentWidget : public QWidget { Q_OBJECT public: TransparentWidget(QWidget *parent = 0) : QWidget(parent) { setWindowTitle(QString::fromLocal8Bit("透明無邊框窗口")); setFixedSize(480, 320); setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); QPushButton *button = new QPushButton("Hello world!", this); button->setGeometry(5, 5, 80, 40); } void paintEvent(QPaintEvent *) { QPainter painter(this); painter.fillRect(this->rect(), QColor(0, 0, 0, 0x20)); /* 設(shè)置透明顏色 */ } void mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_startPoint = frameGeometry().topLeft() - event->globalPos(); } } void mouseMoveEvent(QMouseEvent *event) { this->move(event->globalPos() + m_startPoint); } private: QPoint m_startPoint; };
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
新聞名稱:使用Qt框架怎么實(shí)現(xiàn)一個(gè)透明無邊框窗口
文章轉(zhuǎn)載:http://fisionsoft.com.cn/article/ghshge.html