當(dāng)前位置:首頁 > 芯聞號 > 充電吧
[導(dǎo)讀]1. QPalette的方法int?main(int?argc,?char?*argv[]) { ????QApplication?app(argc,argv); ???? ????QFrame?*f

1. QPalette的方法


int?main(int?argc,?char?*argv[])
{
????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();
}

注意圖片路徑怎么表示,我的圖片放在該工程下的images文件夾中。能解決圖片大小和空間大小不一致的問題


2.setStyleSheet方法(非常好用的方法)


#include#includeint?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();
}


注意:很漂亮的效果吧~~注意代碼中紅線的部分噢,設(shè)置ObjectName后,才能保證setStyleSheet只作用在我們的frame上,不影響其子控件的背景設(shè)置。之所以用border-image而不用background-image,還是上面的問題,用background-image不能保證圖片大小和控件大小一致,圖片不能完全顯示,這個以后再補充了,現(xiàn)在還沒有找到方法。


3.paintEvent事件方法


//myframe.h文件
#ifndef?MYFRAME_H
#define?MYFRAME_H

#include#includeclass?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#include#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();
}

注:跟前面一樣的效果吧,與前面的差別就是這個背景圖片不隨著窗口的大小而變化,因為它的固定大小被設(shè)置成(400,700)了。重寫QWidget的paintEvent事件,當(dāng)控件發(fā)生重繪事件,比如show()時,系統(tǒng)就會自動調(diào)用paintEvent函數(shù)


好了,上面是三種設(shè)置背景圖片的方法,下面我要說一個設(shè)置QPushButton的背景圖片的方法,用的是setIcon方法(其實QPushButton設(shè)置背景圖片也可以用前面三種方法的,不過現(xiàn)在這種Icon方法的看起來也不錯)


#include#includeint?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();
}





本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉