首先,不能繼承QMainWindows。由於它自帶佈局器,除非重載他的佈局。 函數
QLabel *a = new QLabel(); 佈局
a->setStyleSheet("background-color:red");上面的代碼是在一個類的構造函數中實現的,該類繼承與QMainWindows。但是結果確沒有出現紅色的背景,我應該怎麼修改? ui
應該是setshown的問題
this
QLabel *a = new QLabel(this); 這樣才能在窗口中顯示 url
修改後的代碼分析以下: spa
//QPainter painter(this);
//painter.setRenderHints(QPainter::Antialiasing| QPainter::TextAntialiasing);
//painter.begin(this);
//QImage a;
//a.load("./10010.jpg");
//QPoint p(30,30);
//painter.drawImage(p,a);
//a.save("ok.jpg");
//painter.end(); 繼承
ui.setupUi(this);//調用系統默認的UI構造,實現菜單欄等(就是那行白框)
QLabel *a = new QLabel(this);//在當前窗口中添加Label控件
a->setStyleSheet("background-color:red");
// a->setShown(true);//若是不在構造函數中指定父級窗口,該Label是默認不顯示的,須要單獨設置才能顯示
QPixmap *c = new QPixmap();
c->load("./10010.png"); get
//路徑一直是個問題,「../」表示上一級目錄開始 「./」表示當前同級目錄開始 「/」表示根目錄開始。 it
//不少時候失敗了。最後用setStyleSheet
// a->setPixmap(*c);
// QImage image;
// image = image.scaled(512,512);
//// image.save("123.png");
// QFile f("123.txt");
// f.open(QIODevice::WriteOnly);
// image.save("123.png");
a->setStyleSheet("QLabel{image:url(10010.jpg)}");
a->setFixedSize(180,224);
QHBoxLayout *b = new QHBoxLayout();
b->addWidget(a);
// this->setLayout(b);
this->setShown(true);
構造函數