使用Qt框架怎么實(shí)現(xiàn)一個(gè)透明無(wú)邊框窗口?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(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 如何無(wú)邊框窗口?
設(shè)置setWindowFlags(Qt::FramelessWindowHint);
即可無(wú)邊框窗口,但無(wú)法移動(dòng)和改變大小。
0x02 如何拖拽窗口?
由于系統(tǒng)窗口被設(shè)置為Qt::FramelessWindowHint
會(huì)導(dǎo)致窗口不能被拖動(dòng)。通過(guò)捕獲鼠標(biāo)移動(dòng)事件從而實(shí)現(xiàn)窗口移動(dòng)。
void mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { /* 捕獲按下時(shí)坐標(biāo) */ m_startPoint = frameGeometry().topLeft() - event->globalPos(); } } void mouseMoveEvent(QMouseEvent *event) { /* 移動(dòng)窗口 */ this->move(event->globalPos() + m_startPoint); }
0x03 完整代碼
#include <QWidget> #include <QVBoxLayout> #include <QPushButton> #include <QPainter> #include <QMouseEvent> class TransparentWidget : public QWidget { Q_OBJECT public: TransparentWidget(QWidget *parent = 0) : QWidget(parent) { setWindowTitle(QString::fromLocal8Bit("透明無(wú)邊框窗口")); 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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
當(dāng)前名稱:使用Qt框架怎么實(shí)現(xiàn)一個(gè)透明無(wú)邊框窗口
瀏覽路徑:http://muchs.cn/article14/ghshge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站內(nèi)鏈、App開發(fā)、網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)