首先,不能继承QMainWindows。因为它自带布局器,除非重载他的布局。
QLabel *a = new QLabel();
a->setStyleSheet("background-color:red");
QHBoxLayout *b = new QHBoxLayout();
b->addWidget(a);
this->setLayout(b);
//ui.setupUi(this);
上面的代码是在一个类的构造函数中实现的,该类继承与QMainWindows。可是结果确没有出现红色的背景,我应该怎么修改?
应该是setshown的问题
QLabel *a = new QLabel(this); 这样才能在窗口中显示
修改后的代码分析如下:
//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控件