----------- MainWindow.h -------------------
class MyPanel: public QWidget
{
Q_OBJECT
public:
explicit MyPanel(QWidget *parent = 0);
~MyPanel();
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MyPanel* m_pWidget;
private:
Ui::MainWindow *ui;
};
----------- MainWindow.cpp -------------------
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pWidget = new MyPanel(this);
m_pWidget->move(0, 0);
m_pWidget->resize(300, 300);
m_pWidget->show();
m_pWidget->setStyleSheet("QWidget{border-color: rgb(228, 230, 233);}");
}
自己写了个继承QWidget的MyPanel,为啥用setStyleSheet设置背景颜色不管用?
但是把m_pWidget 换成QWidget类型的就可以......
---------------------------------------------------------------------------
void MyPanel::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
已经解决,少了这个函数
http://www.qtcn.org/bbs/read-htm-tid-62088.html