Border for Class inheriting QWidget and QDialog
-
Hi,
I have a class inheriting from QWidget and QDialog,
i require border for outer widget not to all children inside.How can i achieve this?.
Thanks,
-
Hi,
Why the double inheritance ? QDialog is already a QWidget.
-
Hi,
No i have inherited from QWidget,
sorry about question. rephrased
i asked if we have inherited either from QDialog or QWidget, how can we have border for the main widget not to its child ?.if i use
this.setStyleSheet("QWidget{border:2px solid black}");
or
this.setStyleSheet("QDialog{border:2px solid black}");
it is providing border for the child widgets too, reading the docs i came to know this cant be done.
I only want to have border for main widget, not for its children.
Please provide guidance,
Thanks,
-
Just name your parent object with ->setObjectName("Pradeep") then use object name selector # in the css.
#Pradeep {
}
-
Hi,
I used
this->setObjectName("hello"); this->setWindowFlags(Qt::FramelessWindowHint); this->setStyleSheet("#hello{border:2px solid black}");
still not working , i am not getting border for MyWidget
Thanks,
-
Hi,
I used
this->setObjectName("hello"); this->setWindowFlags(Qt::FramelessWindowHint); this->setStyleSheet("#hello{border:2px solid black}");
still not working , i am not getting border for MyWidget
Thanks,
@Pradeep-Kumar said in Border for Class inheriting QWidget and QDialog:
Hi
Well test some more. It does work.
If I place widget and do
ui->widget->setObjectName("hello");
ui->widget->setWindowFlags(Qt::FramelessWindowHint);
ui->widget->setStyleSheet("#hello{border:2px solid black}");i get
So naming the widget does work :)
-
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setObjectName("hello");
this->setWindowFlags(Qt::FramelessWindowHint);this->setStyleSheet( "#hello{" "border: 2px solid grey;" "padding: 6px; }" );
}
My Class Name is Widget , how to make use of it?.
Thanks,
-
Hi,
IIRC:
"Widget{" "border: 2px solid grey;" "padding: 6px; }"
I'd suggest giving your class a more sensible name (even MyWidget will do). I can already see the hair pulling debugging session because nobody notices there's a missing Q before Widget.
-
You can draw a rectangle of your screen size it can be looks like border.
drawRect(this->rect())
-
Hi,
if i use paintevent to get the border for the main widget.
void Widget::paintEvent(QPaintEvent *event)
{QPainter painter(this); QPen oMainBorderQPen; oMainBorderQPen.setWidth(1); oMainBorderQPen.setColor("black"); painter.setPen(oMainBorderQPen); painter.drawRect(this->rect());
}
i am getting the border only for top and left , but for bottom and right i am no getting the border.
image is attached
https://i.imgsafe.org/3e800d2ed5.pngAny Solutions?.
Thanks,
-
Hi,
if i use paintevent to get the border for the main widget.
void Widget::paintEvent(QPaintEvent *event)
{QPainter painter(this); QPen oMainBorderQPen; oMainBorderQPen.setWidth(1); oMainBorderQPen.setColor("black"); painter.setPen(oMainBorderQPen); painter.drawRect(this->rect());
}
i am getting the border only for top and left , but for bottom and right i am no getting the border.
image is attached
https://i.imgsafe.org/3e800d2ed5.pngAny Solutions?.
Thanks,
@Pradeep-Kumar if you set the width and height 1 pixel less you might see it. I have had to do that in the past.