贴下源代码
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
class PaintedWidget : public QWidget
{
public:
PaintedWidget();
protected:
void paintEvent(QPaintEvent *event);
};
mian.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
PaintedWidget w;
w.show();
return app.exec();
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
PaintedWidget::PaintedWidget()
{
int width=QApplication::desktop()->width();//自动获取长度
int height=QApplication::desktop()->height();//自动获取宽
this->setFixedSize(width,height);
setWindowTitle(tr("RTU text"));
}
void PaintedWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setBrush(Qt::darkGreen);
painter.drawRect(100, 170, 25, 10);
painter.drawRect(40, 200, 20, 10);
}
------解决思路----------------------
直接加就行,使用layout或者直接把子控件的父指向你的控件。子控件会在父上面,父绘制的图形也会在子控件下面。
------解决思路----------------------
最简单的就是在你的ui设计器上面选中你需要设置的控件,然后右键你会看见一个放在后面和前面,