@Munkhtamir
you have to create it after the other Widget, for it to be on top by default, otherwise you can call raise() on your grid-widget to make it the top most item.
But I force a problem for after this, and thatone is because of transparency/opacity
inside paintEvent you should call the base implementation
QWidget::paintEvent(event);
QPainter painter(this);
....
That way your widget will react to style sheets, and you can set a simple stylesheet like
setStyleSheet(background-color:transparent;);
Edit: example:
//Custom class .h
#include <QWidget>
class GridWidget : public QWidget
{
Q_OBJECT
public:
explicit GridWidget(QWidget *parent = nullptr);
void setGridDistance(int distance);
protected:
void paintEvent(QPaintEvent *event);
int m_GridDistance = 20;
};
//Customclass .cpp
#include "gridwidget.h"
#include <QPainter>
GridWidget::GridWidget(QWidget *parent) : QWidget(parent)
{
setStyleSheet("background-color:transparent;");
}
void GridWidget::setGridDistance(int distance)
{
m_GridDistance = distance;
update();
}
void GridWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter p(this);
p.setPen(QPen(Qt::white,3));
for(int i(0); i < width(); i += m_GridDistance){
p.drawLine(i,0,i,height());
}
for(int i(0); i < height(); i += m_GridDistance){
p.drawLine(0,i,width(),i);
}
}
//Example main.cpp black widget with GridWidget ontop;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget();
w->setStyleSheet("background-color:black");
w->resize(200,200);
w->show();
QHBoxLayout *layout = new QHBoxLayout(w);
layout->setMargin(0);
layout->addWidget(new GridWidget());
return a.exec();
}