一、
QT 透明設置
背景,標題欄透明,下級Widget,painter繪出來的(比如,drawtext,drawline)不透明
QWidget window; window.setWindowFlags(Qt::FramelessWindowHint); window.setAttribute(Qt::WA_TranslucentBackground,true);
全部透明,具體包括標題欄,背景,下級Widget
QWidget window;
window.setWindowOpacity(0.7); //設置透明度,范圍0 至 1.0
二、
1. QPalette的方法
#include < QtGui >
int main( int argc, char * argv[])
{
QApplication app(argc,argv);
QFrame * frame = new QFrame;
frame -> resize( 400 , 700 );
QPixmap pixmap("images/frame.png");
QPalette palette;
palette.setBrush(frame -> backgroundRole(),QBrush( pixmap ));
frame -> setPalette(palette);
frame->setMask(pixmap.mask()); //可以將圖片中透明部分顯示為透明的
frame -> setAutoFillBackground( true );
frame -> show();
return app.exec();
}
注意圖片路徑怎么表示,我的圖片放在該工程下的images文件夾中。
存在問題:圖片可以顯示出來,但是圖片大小不能和frame大小一致,顯示效果不好,具體怎樣調整大小,以后再補充,效果如下(設置了透明的,好像很漂亮~透明部分將我的桌面顯示出來了~_~):
2.setStyleSheet方法(非常好用的方法)
#include < QtGui >
int main( int argc, char * argv[])
{
QApplication app(argc,argv);
QFrame * frame = new QFrame;
frame ->setObjectName("myframe" );
frame -> resize( 400 , 700 );
frame -> setStyleSheet( " QFrame#myframe{border-image:url(images/frame.png)} " );
frame -> show();
return app.exec();
}
效果如下:
注意:很漂亮的效果吧~~注意代碼中紅線的部分噢,設置ObjectName后,才能保證set StyleSheet 只作用在我們的frame上,不影響其子控件的背景設置。之所以用border-image而不用background-image,還是上面的問題,用 background-image不能保證圖片大小和控件大小一致,圖片不能完全顯示,這個以后再補充了,現在還沒有找到方法。
3.paintEvent事件方法
#ifndef MYFRAME_H
#define MYFRAME_H
#include < QWidget >
#include < QtGui >
class MyFrame : public QWidget
{
public :
MyFrame();
void paintEvent(QPaintEvent * event );
};
#endif // MYFRAME_H
// myframe.cpp文件
#include " myframe.h "
MyFrame::MyFrame()
{
}
void MyFrame::paintEvent(QPaintEvent * event )
{
QPainter painter( this );
painter.drawPixmap( 0 , 0 , 400 , 700 ,QPixmap( " images/frame.png " ));
}
// main.cpp文件
#include < QApplication >
#include < QtGui >
#include " myframe.h "
int main( int argc, char * argv[])
{
QApplication app(argc,argv);
MyFrame * frame = new MyFrame;
frame -> resize( 400 , 700 );
frame -> show();
return app.exec();
}
效果如下:
注:跟前面一樣的效果吧,與前面的差別就是這個背景圖片不隨着窗口的大小而變化,因為它的固定大小被設置成(400,700)了。重寫QWidget的paintEvent事件,當控件發生重繪事件,比如show()時,系統就會自動調用paintEvent函數。
好了,上面是三種設置背景圖片的方法,下面我要說一個設置QPushButton的背景圖片的方法,用的是setIcon方法(其實QPushButton設置背景圖片也可以用前面三種方法的,不過現在這種Icon方法的看起來也不錯)
#include < QtGui >
int main( int argc, char * argv[])
{
QApplication app(argc,argv);
QFrame * frame = new QFrame;
QPushButton * button0 = new QPushButton(frame);
QPushButton * button1 = new QPushButton(frame);
QPushButton * button2 = new QPushButton(frame);
QPushButton * button3 = new QPushButton(frame);
QPushButton * button4 = new QPushButton(frame);
QPushButton * button5 = new QPushButton(frame);
frame -> setObjectName( " myframe " );
frame -> resize( 400 , 700 );
frame -> setStyleSheet( " QFrame#myframe{border-image:url(images/frame.png)} " );
button0 -> setGeometry( 60 , 150 , 68 , 68 );
button1 -> setGeometry( 160 , 150 , 68 , 68 );
button2 -> setGeometry( 260 , 150 , 68 , 68 );
button3 -> setGeometry( 60 , 280 , 68 , 68 );
button4 -> setGeometry( 160 , 280 , 68 , 68 );
button5 -> setGeometry( 260 , 280 , 68 , 68 );
QIcon icon;
QPixmap pixmap0( " images/SMS.png " );
icon.addPixmap(pixmap0);
button0 -> setIcon(icon);
button0 -> setIconSize(QSize( 68 , 68 ));
button0 -> setFixedSize(pixmap0.size());
button0 -> setMask(pixmap0.mask());
QPixmap pixmap1( " images/EMail.png " );
icon.addPixmap(pixmap1);
button1 -> setIcon(icon);
button1 -> setIconSize(QSize( 68 , 68 ));
button1 -> setFixedSize(pixmap1.size());
button1 -> setMask(pixmap1.mask());
QPixmap pixmap2( " images/Contacts.png " );
icon.addPixmap(pixmap2);
button2 -> setIcon(icon);
button2 -> setIconSize(QSize( 68 , 68 ));
button2 -> setFixedSize(pixmap2.size());
button2 -> setMask(pixmap2.mask());
QPixmap pixmap3( " images/Calendar.png " );
icon.addPixmap(pixmap3);
button3 -> setIcon(icon);
button3 -> setIconSize(QSize( 68 , 68 ));
button3 -> setFixedSize(pixmap3.size());
button3 -> setMask(pixmap3.mask());
QPixmap pixmap4( " images/GoogleVoice.png " );
icon.addPixmap(pixmap4);
button4 -> setIcon(icon);
button4 -> setIconSize(QSize( 68 , 68 ));
button4 -> setFixedSize(pixmap4.size());
button4 -> setMask(pixmap4.mask());
QPixmap pixmap5( " images/AndroidMarket.png " );
icon.addPixmap(pixmap5);
button5 -> setIcon(icon);
button5 -> setIconSize(QSize( 68 , 68 ));
button5 -> setFixedSize(pixmap5.size());
button5 -> setMask(pixmap5.mask());
frame -> show();
return app.exec();
}
注:圖標效果不錯吧~_~
好了,今天就寫到這里,以后有新的內容再補充。
補充,這樣就可以讓圖片跟窗口一樣大小了。
{
QApplication app(argc,argv);
QFrame * frame = new QFrame;
frame -> resize( 400 , 700 );
QImage image1;
image1.load( " images/frame1.jpg " );
QImage image2 = image1.scaled( 400 , 700 );
QPalette palette;
palette.setBrush(frame -> backgroundRole(),QBrush(image2));
frame -> setPalette(palette);
frame -> setMask(pixmap.mask()); // 可以將圖片中透明部分顯示為透明的
frame -> setAutoFillBackground( true );
frame -> show();
return app.exec();
}
三、
四、
【如果其父窗口為空的話,透明的地方會成為黑塊。】
很多朋友都問透明的效果怎么做,為什么自己做的無邊框窗體不可移動,一個個回答的很累,干脆寫出來分享下好了,我只用代碼說話。
- //main.cpp
- int main(int argc, char *argv[])
- {
- QApplication::setStyle("cleanlooks");
- QApplication a(argc, argv);
- login w;
- w.setWindowTitle("ClientLogin");
- w.setWindowOpacity(1);
- w.setWindowFlags(Qt::FramelessWindowHint);
- w.setAttribute(Qt::WA_TranslucentBackground);
- w.show();
- w.move(200,100);
- return a.exec();
- }
關鍵的語句,就是其中的
- w.setWindowOpacity(1);
- w.setWindowFlags(Qt::FramelessWindowHint);
- w.setAttribute(Qt::WA_TranslucentBackground);
這些語句,不知道什么意思就摁下F1,或者直接查閱幫助文檔……
對窗體無邊框的設置要寫在main里面,這樣所有派生的子窗口,QDialog,QWidget都可繼承, 很好規划和管理,方便統一美化設計。以工程中一個聊天窗口為例,先用PS制作一個窗體的背景圖片,注意存為png格式,這是透明的關鍵。不會使PS,可以找些PNG資源圖片。 我的PNG透明背景圖為:

將它添加到你的資源包中,然后設置為窗體的背景。
下圖是我的工程,其中的場景設置其實也是更換組建的背景圖片嘍~~ 
這個你就可以預覽到透明的無邊框窗體了,但是還有一個重要的問題,窗口竟然無法移動。
這也是無邊框導致的……具體原因我不細說,搜一下很清晰,我只說解決方案。
在每個子窗口中,都添加:
- void yourwindow::mousePressEvent(QMouseEvent *event){
- this->windowPos = this->pos();
- this->mousePos = event->globalPos();
- this->dPos = mousePos - windowPos;
- }
- void yourwindow::mouseMoveEvent(QMouseEvent *event){
- this->move(event->globalPos() - this->dPos);
- }
- void yourwindow::changeEvent(QEvent *e){
- QMainWindow::changeEvent(e);
- switch (e->type()) {
- case QEvent::LanguageChange: ui->retranslateUi(this);
- break;
- default:
- break;
- }
- }
這樣就大功告成了,運行一下看看效果,綠森林是俺滴桌面,可以忽略之。
歡迎交流
一、將Qt窗口嵌入到桌面中。
聲明一個最簡單的類:
class Dialog :
public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
}
函數實現:
Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
//創建個LineEdit用來測試焦點
QLineEdit* le =
new QLineEdit(
this);
}
Dialog::~Dialog()
{
}
主函數:
int main(
int argc,
char *argv[])
{
QApplication a(argc, argv);
Dialog w;
HWND desktopHwnd = findDesktopIconWnd();
if(desktopHwnd) SetParent(w.winId(), desktopHwnd);
w.show();
return a.exec();
}
運行效果:
有個窗口嵌入了桌面。按win+D組合鍵可以看到此窗口在桌面上。
二、讓窗口全透明:
2.1最容易想到的就是setWindowOpacity()函數了。
w.setWindowOpacity(0.5),運行:結果杯具了,此函數完全無效,因為其父窗口特殊,這個函數內部使用的系統窗口標志不被支持。
2.2
w.setAttribute(Qt::WA_TranslucentBackground, true);
運行效果:
全透明ok。如果其父窗口為空的話,透明的地方會成為黑塊。
三、讓窗口半透明
3.1w.setAttribute(Qt::WA_TranslucentBackground, true) + 背景調色板
運行效果仍然是全透明,因為TranslucentBackground為true,根本不畫背景。
3.2單純的背景調色板:
QPalette pal = w.palette();
pal.setColor(QPalette::Background, QColor(100,100,100,50));
w.setPalette(pal);
w.setAutoFillBackground(
true);
運行效果出現了半透明:
但是還沒大功告成,不停點擊桌面,再點擊這個窗口,會發現這個窗口越來越不透明,直至完全不透明了。不知道是不是qt的bug。
ps:加一句 w.setAttribute(Qt::WA_OpaquePaintEvent,true); 窗口就能夠一直保持這個效果了。即這個方案可行。
pps:此方案在XP也是黑色底塊。
3.3轉戰paintEvent()
protected:
void paintEvent(QPaintEvent *);
void Dialog::paintEvent(QPaintEvent *e)
{
QPainter p(
this);
p.fillRect(rect(), QColor(0,0xff,0,30));
}
用一個帶有alpha值的顏色填充背景,運行效果發現顏色確實有alpha值,但是桌面的內容透不過來。
3.4setAttribute(Qt::WA_TranslucentBackground, true) + paintEvent()
運行效果:
得到了設想中的效果。
最終的主函數代碼:
int main(
int argc,
char *argv[])
{
QApplication a(argc, argv);
Dialog w;
HWND desktopHwnd = findDesktopIconWnd();
if(desktopHwnd) SetParent(w.winId(), desktopHwnd);
w.setAttribute(Qt::WA_TranslucentBackground, true);
w.show();
return a.exec();
}
最終的dialog實現代碼:
Dialog::Dialog(QWidget *parent) : QWidget(parent)
{
//創建個LineEdit用來測試焦點
QLineEdit* le =
new QLineEdit(
this);
}
Dialog::~Dialog()
{
}
void Dialog::paintEvent(QPaintEvent *e)
{
QPainter p(
this);
p.fillRect(rect(), QColor(0,0xff,0,30));
}
PS:
經測試此代碼在XP運行不正常。窗口成為黑色背景塊。只能是顏色半透明了。
還有就是圖標會被蓋住。只能把w.setAttribute(Qt::WA_TranslucentBackground, true);注釋掉,有半透明顏色,無法看到桌面。
五、
QPalette pal = palette();
pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));
setPalette(pal);
setAttribute(Qt::WA_TranslucentBackground,
true);
setWindowOpacity(0.7)
void TestWindow::paintEvent( QPaintEvent* )
{
QPainter p(
this);
p.setCompositionMode( QPainter::CompositionMode_Clear );
p.fillRect( 10, 10, 300, 300, Qt::SolidPattern );
}
本文出自 “悠然” 博客,請務必保留此出處http://mypyg.blog.51cto.com/820446/172661
六、
本文主要是解決Qt中QGraphicsAbstractShapeItem,QPixmap,QPainter等組件的透明化顯示問題。
在Qt中定義了一個常量,用於設置透明的顏色,即Qt::transparent,表示RGBA值為(0,0,0,0)的透明色。
在QGraphicsAbstractShapeItem的集成類中,可以把某些部分畫成Qt::transparent來設置透明。
在QPainter中,可以通過setBackgroundMode()選擇兩種不同的背景模式,一種是Qt::TransparentMode,另外一種是Qt::OpaqueMode。在Qt::TransparentMode模式下,背景本身就是透明的,此時QPainter的setBackground()函數不起作用;在Qt::OpaqueMode模式下,通過QPainter的setBackground()函數來設置QPainter類對象的背景色,在此模式下,也可以設置背景色為Qt::transparent。
有時,我們要把QPainter畫到QPixmap中以防止閃爍。如果需要設置QPixmap的透明,可以先用QPixmap的fill()函數先把場景設置成透明的(fill(Qt::transparent) )。
QImage::fill ( uint pixelValue)
試試用這個填充透明色。
theImage.fill(qRgba(255,255,255,0));
七、
學習Qt編程,有時候我們很想做出好看又比較炫的畫面,這時就常用到qt上的一些技巧。
這里我以一個小例子來展示qt的這些技巧,此qt編程寫的,如圖:(去掉標題欄和設置窗口透明后)

代碼實現部分:
.h文件
- <span style="font-size:14px;">#ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include<QLabel>
- #include <QMouseEvent>
- #include<QPalette>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- private slots:
- void on_pushButton_Set_clicked();
- };
- #endif // MAINWINDOW_H
- </span>
mainwindow.cpp
- <span style="font-size:14px;">#include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- this->setWindowTitle("QQ ");
- this->setWindowIcon(QIcon(":/images/po.jpg"));
- this->setWindowFlags(Qt::FramelessWindowHint);//去掉標題欄
- this->setGeometry(QRect(950, 55, 350, 250));//可設置窗口顯示的方位與大小
- //this->setWindowOpacity(0.7);//設置透明1-全體透明
- this->setAttribute(Qt::WA_TranslucentBackground, true);//設置透明2-窗體標題欄不透明,背景透明
- this->resize(300,300);//顯示大小
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- </span>
main.cpp文件
- <span style="font-size:14px;">#include <QtGui/QApplication>
- #include <QTextCodec>
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
- QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
- QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
- MainWindow w;
- w.show();
- return a.exec();
- }
- </span>
本例中用到的透明是 : 窗體標題欄不透明,背景透明。
這里介紹幾種設置透明效果的用法:
1.this->setWindowOpacity(0.7);//全體透明(指的是窗體,標題欄以及上面所有的控件都透明)里面的參數可以控制透明度。
2.窗口整體透明,但是窗體上的控件不透明。 通過設置窗體的背景色來實現,將背景色設置為全透:
代碼如下:
- <span style="font-size:14px;"> pal = palette();
- pal.setColor(QPalette::background, QColor(0x00,0xff,0x00,0x00));
- setPalette(pal);</span>
3.窗體標題欄不透明,背景透明。(本例中用到的)
this->setAttribute(Qt::WA_TranslucentBackground,true);
4.窗口整體不透明,局部透明:在Paint事件中使用Clear模式繪圖。
- <span style="font-size:14px;">void mainwindow::paintEvent( QPaintEvent* )
- { QPainter p(this);
- p.setCompositionMode( QPainter::CompositionMode_Clear );
- p.fillRect( 30, 30, 300, 300, Qt::SolidPattern );
- }
- </span>
繪制區域全透明,如果繪制區域有控件不會影響控件的透明。
5.這里說一下本程序中怎樣去掉標題欄
this->setWindowFlags(Qt::FramelessWindowHint);//去掉標題欄
轉載注明:http://blog.csdn.net/liuyang1990i/article/details/8227342

