只上代码,不解释。只需修改main.h和main.cpp。
main.h:
#ifndef MAIN_H
#define MAIN_H
#include
#include
#include
#include "ui_mainwindow.h"
class SetBackground : public QMainWindow
{
Q_OBJECT
public:
SetBackground(QWidget *parent = 0);
void SetBackgroundImage();
private:
Ui::MainWindow ui;
};
#endif // MAIN_H
main.cpp:
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SetBackground w;
w.showMaximized();
return a.exec();
}
SetBackground::SetBackground(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle("MySetbackground");
SetBackgroundImage();
}
void SetBackground::SetBackgroundImage()
{
//Using QPalette you can set background image as follows.
QPalette p = palette();
//Load image to QPixmap, Give full path of image
QPixmap pixmap1("C://DATA//Images//20100629.jpg"); //For emulator C: is ../epoc32/winscw/c so image must be at that location
//resize image if it is larger than screen size.
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect rect = desktopWidget->availableGeometry();
QSize size(rect.width() , rect.height());
//resize as per your requirement..
QPixmap pixmap(pixmap1.scaled(size));
p.setBrush(QPalette::Background, pixmap);
setPalette(p);
}
然后在Nokia真机上运行就出现一个名为MySetbackground的简单窗口,窗口里面是我们自己事先放好的图片20100629.jpg。