/**********************************轉載分割線**********************************/
Qt 的一些心得
本文來自:http://blog.csdn.net/lpt19832003/archive/2010/03/15/5381095.aspx
一. 背景刷成黑色,前景色設爲白色。
方法一、paltette方式,經測試,該方法不會影響到其他控件,推薦使用
點擊(此處)摺疊或打開
-
QPalette bgpal = palette();
-
bgpal.setColor (QPalette::Background, QColor (0, 0 , 0, 255));
-
//bgpal.setColor (QPalette::Background, Qt::transparent);
- bgpal.setColor (QPalette::Foreground, QColor (255,255,255,255)); setPalette (bgpal);
方法二、stylesheet方式
影響子控件的方法是:
點擊(此處)摺疊或打開
- setStyleSheet ("background-color: rgb(0,0,0);color: rgb(255,255,255);");
不影響子控件的方法是:
點擊(此處)摺疊或打開
- setStyleSheet ("venus--TitleBar {background-color: rgb(0,0,0);color: rgb(255,255,255);}");
點擊(此處)摺疊或打開
-
this->setObjectName("pw");// 設置對象句,相當於css裏的id
- this->setStyleSheet("#pw {border-image: url(:/new/prefix1/Pic/54.png);}");// 設置id對應元素的背景色
二. 圓角控件 用stylesheet方式
點擊(此處)摺疊或打開
- setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;");
三. 圓角窗口
點擊(此處)摺疊或打開
-
RoundRectWin::RoundRectWin()
- {
-
QPalette p = palette();
-
QPixmap img("roundrect.png");
-
QBitmap mask("roundrect_mask.png");
-
p.setBrush(QPalette::Window, QBrush(img));
-
setPalette(p);
-
setMask(mask);
-
resize(img.size());
-
//setWindowFlags(Qt::FramelessWindowHint);//這句會去掉標題欄 } 注意:mask的圖多餘部分設爲白色
- }
四. 半透明窗口
1.窗口整體透明,但是窗體上的控件不透明。 通過設置窗體的背景色來實現,將背景色設置爲全透。
點擊(此處)摺疊或打開
-
QPalette pal = palette();
-
pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));
- setPalette(pal);
注:窗體標題欄不透明,窗體客戶區上的控件不透明,QLabel控件只是字顯示,控件背景色透明; 窗體客戶區完全透明。
另外從網上看到的方法:
點擊(此處)摺疊或打開
- setAttribute(Qt::WA_TranslucentBackground, true);
2.窗口及其上面的控件都半透明:
點擊(此處)摺疊或打開
- setWindowOpacity(0.7);
3.窗口整體不透明,局部透明:
在Paint事件中使用Clear模式繪圖。
點擊(此處)摺疊或打開
-
void TestWindow::paintEvent( QPaintEvent* )
-
{
-
QPainter p(this);
-
p.setCompositionMode( QPainter::CompositionMode_Clear );
-
p.fillRect( 10, 10, 300, 300, Qt::SolidPattern );
- }
試驗效果:繪製區域全透明。如果繪製區域有控件不會影響控件。
以上實驗均是基於Directfb的Qte測試。在Linux下及Windows下效果會略有不同。
比如在Windows下第3個實驗,繪製區域就是一個黑窟窿,如果Window本身半透,則是Window下面的內容加上半透效果,不是全透明效果。
五. 控制QPixmap的alpha
點擊(此處)摺疊或打開
-
QPixmap temp(pixmapTop.size()); temp.fill(Qt::transparent);
-
QPainter p(&temp);
-
p.setCompositionMode(QPainter::CompositionMode_Source);
-
p.drawPixmap(0, 0, pixmapTop);
-
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
-
p.fillRect(temp.rect(), QColor(0, 0, 0, alpha)); //--lable顯示前景圖片
ui->label->setScaledContents(true);
- ui->label->setPixmap(temp);
六. layout 的邊界
點擊(此處)摺疊或打開
- layout->setMargin (0);
終於把qt半透明搞定了
本文來自:http://hi.baidu.com/sszllx/blog/item/ab833a59ea98622f2934f001.html點擊(此處)摺疊或打開
-
QFrame *frame = new QFrame();
-
QPixmap grabPixmap = QPixmap::grabWindow( frame->winId(), 20, 0, 100, 100 );
-
frame->setGeometry( 20, 20, 100, 100 );
-
frame->hide();
-
-
QPixmap pixmap = Resource::loadPixmap( "qpelogo" );
-
-
QImage image = pixmap.convertToImage();
-
//image.convertDepth( 32 );
-
image.setAlphaBuffer( true );
-
QImage bgImage = grabPixmap.convertToImage();
-
//bgImage.convertDepth( 32 );
-
bgImage.setAlphaBuffer( true );
-
-
double k = 0.5;
-
QRgb *rgbpos1 = image.colorTable();
-
QRgb *rgbpos2 = bgImage.colorTable();
-
for( int y = 0; y < 80; y++ )
-
for( int x = 0; x < 80; x++ )
-
{
-
#if 1
-
uint rgb1 = image.pixel( x, y );
-
uint rgb2 = bgImage.pixel( x, y );
-
unsigned int red = qRed(rgb1);
-
unsigned int green = qGreen(rgb1);
-
unsigned int blue = qBlue(rgb1);
-
unsigned int bgred = qRed(rgb2);
-
unsigned int bggreen = qGreen(rgb2);
-
unsigned int bgblue = qBlue(rgb2);
-
red = k * red + ( 1 - k ) * bgred;
-
green = k * green + ( 1 - k ) * bggreen;
-
blue = k * blue + ( 1 - k ) * bgblue;
-
//red = red << 4;
-
//green = green << 4;
-
//blue = blue << 4;
-
#endif
-
//color = QColor( red, green, blue );
-
//unsigned int myrgb = color.rgb();
-
//image.setPixel( x, y, myrgb );
-
uint *p = (uint *)image.scanLine(y) + x;
-
*p = qRgb( red, green, blue );
-
-
}
-
-
qWarning("%d %d", image.depth(), bgImage.depth());
-
grabPixmap.convertFromImage( image );
-
-
frame->setBackgroundPixmap( grabPixmap );
-
frame->show();
- #endif
將窗口嵌入桌面
ps:這兒用上了前面一文提到的函數findDesktopIconWnd()。
本文來自:http://mypyg.blog.51cto.com/820446/263369
一、將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();
- }
二、讓窗口全透明:
2.1最容易想到的就是setWindowOpacity()函數了。
點擊(此處)摺疊或打開
- w.setWindowOpacity(0.5);
2.2
點擊(此處)摺疊或打開
- w.setAttribute(Qt::WA_TranslucentBackground, true);
三、讓窗口半透明
3.1
點擊(此處)摺疊或打開
- w.setAttribute(Qt::WA_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()
點擊(此處)摺疊或打開
-
void Dialog::paintEvent(QPaintEvent *e)
-
{
-
QPainter p(this);
-
p.fillRect(rect(), QColor(0,0xff,0,30));
- }
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(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));
- }
經測試此代碼在XP運行不正常。窗口成爲黑色背景塊。只能是顏色半透明瞭。
還有就是圖標會被蓋住。只能把w.setAttribute(Qt::WA_TranslucentBackground, true);註釋掉,有半透明顏色,無法看到桌面。
在QT中自繪控件的方法說明
本文來自:http://blog.csdn.net/dotboy/archive/2009/06/22/4287805.aspx
首先給出在QT中進行標題欄自繪的方法(兩個網址給出了方法和參考代碼)
litterflybug的專欄 ::http://blog.csdn.net/litterflybug/archive/2009/05/07/4157482.aspx
QT官方FAQ: http://www.qtsoftware.com/developer/faqs/faq.2007-04-25.2011048382
其次,做一點關於在windows平臺下將頂級窗口透明化的說明
1.在4.5版本以前,要實現Top level窗口的透明化,需要使用setMask方法,該方法有兩個重載,一個是QRegion定義一個區域表明這一區域是要被繪製的區域,在該區域以外的地方是不需要繪製的.第二個重載版本是需要一個QBitmap,這個QBitmap只需要一張單色位圖就可以了(另外,一張帶有透明效果的PNG圖片被讀入QPixmap中以後,可以使用函數mask()返回一張與原圖對應的單色位圖,這很方便實際使用),白色部分表示是不透明要被繪製的區域,黑色部分表示該區域不應該被繪製(能實現透明的效果).
2.4.5版本發行以後,可以設置窗口屬性Qt::WA_TranslucentBackground來設定該窗口透明顯示,但是有一個問題,就是如果該窗口是個top level窗口,就會有標題欄(title bar), 在windows平臺下,如果不把這個標題欄去掉(設置Qt::FramelessWindowHint 標誌位就會去掉標題欄),那麼將不會是透明效果,而是背景爲全黑(我的實驗結果是這樣)
qt 設置子控件爲半透明和背景色問題
本文來自:http://bbs.csdn.net/topics/370078118在子widget的構造函數裏添加如下幾句:
點擊(此處)摺疊或打開
-
QPalette myPalette;
-
QColor myColor(0,0,0);
-
myColor.setAlphaF(0.2);
-
myPalette.setBrush(backgroundRole(),myColor);
-
this->setPalette(myPalette);
- this->setAutoFillBackground(true);
改子widget的背景色 :
點擊(此處)摺疊或打開
-
QPalette myPalette;
-
QColor myColor(0,0,0);
-
myPalette.setBrush(backgroundRole(),myColor);
-
this->setPalette(myPalette);
- this->setAutoFillBackground(true);//這句很關鍵,缺少的話,背景色修改不成功
修改alpha值:
點擊(此處)摺疊或打開
- myColor.setAlphaF(0.2);//通過設置alpha值來改變透明度
qt 窗口圓角問題
本文來自:http://blog.csdn.net/fariel_zhang/article/details/6315463
最近在用qss做界面美化的工作,發現一個問題就是qss不能對頂級窗口設置邊角圓弧,於是得另闢蹊徑。據網上搜索可得到的方法我實現了三種:
1.
點擊(此處)摺疊或打開
-
setWindowFlags(Qt::FramelessWindowHint);
-
QBitmap bmp(this->size());
-
bmp.fill();
-
QPainter p(&bmp);
-
// p.setPen(Qt::NoPen);
-
// p.setBrush(Qt::black);
-
p.setRenderHint(QPainter::Antialiasing);
-
//p.drawRoundedRect(bmp.rect(), 20, 20); //四個角都是圓弧
-
//只要上邊角圓弧
-
int arcR = 20;
-
QRect rect = this->rect();
-
QPainterPath path;
-
//逆時針
-
path.moveTo(arcR, 0);
-
path.arcTo(0, 0, arcR * 2, arcR * 2, 90.0f, 90.0f);
-
path.lineTo(0, rect.height());
-
path.lineTo(rect.width(), rect.height());
-
path.lineTo(rect.width(), arcR);
-
path.arcTo(rect.width() - arcR * 2, 0, arcR * 2, arcR * 2, 0.0f, 90.0f);
-
path.lineTo(arcR, 0);
-
p.drawPath(path);
-
p.fillPath(path, QBrush(Qt::Red)); //arm和windows平臺沒有這行代碼將顯示一個透明的空空的框
- setMask(bmp);
2.
構造函數中
點擊(此處)摺疊或打開
-
setAttribute(Qt::WA_TranslucentBackground, true);
-
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint
- | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
點擊(此處)摺疊或打開
-
QPainter p(this);
- p.drawRoundedRect(0, 0, width() - 1, height() - 1, 20, 20);
3.背景圖片
點擊(此處)摺疊或打開
-
QPixmap pixmap(":/images/*.png");
-
setMask(pixmap.mask());
-
QPalette palette;
-
palette.setBrush(QPalette::Background, QBrush( pixmap ) );
-
setPalette(palette);
-
resize( pixmap.size() );
- setMask(pixmap.mask());