QFrame does not display in UI properly
-
wrote on 16 Nov 2022, 11:31 last edited by Dummie1138
I have some code that places a QFrame and gives it the size of a QRect.
QRect *lineRect = new QRect(20, 160, 400, 10); QFrame *mainLine = new QFrame(this); mainLine->setFrameRect(*lineRect); mainLine->setFrameStyle(QFrame::Raised); qDebug() << *lineRect; qDebug() << mainLine->frameRect();
I have a warning that mainLine is not used, which I'm assuming means none of the functions are actually affecting mainLine.
And right now, the issue is that mainLine-> seems not to affect any change to mainLine. I am not sure why that might be happening. Please let me know if more information is required. -
I have some code that places a QFrame and gives it the size of a QRect.
QRect *lineRect = new QRect(20, 160, 400, 10); QFrame *mainLine = new QFrame(this); mainLine->setFrameRect(*lineRect); mainLine->setFrameStyle(QFrame::Raised); qDebug() << *lineRect; qDebug() << mainLine->frameRect();
I have a warning that mainLine is not used, which I'm assuming means none of the functions are actually affecting mainLine.
And right now, the issue is that mainLine-> seems not to affect any change to mainLine. I am not sure why that might be happening. Please let me know if more information is required.@Dummie1138 said in QFrame is unused:
And right now, the issue is that mainLine-> seems not to affect any change to mainLine
What does this mean please?
Where is this code located?
You also did not call show() on mainLine to actually show it...If you read https://doc.qt.io/qt-6/qframe.html#frameRect-prop you will see this:
"Setting the rectangle doesn't cause a widget update".
I suggested to you to use setGeometry() in your other thread... -
I have some code that places a QFrame and gives it the size of a QRect.
QRect *lineRect = new QRect(20, 160, 400, 10); QFrame *mainLine = new QFrame(this); mainLine->setFrameRect(*lineRect); mainLine->setFrameStyle(QFrame::Raised); qDebug() << *lineRect; qDebug() << mainLine->frameRect();
I have a warning that mainLine is not used, which I'm assuming means none of the functions are actually affecting mainLine.
And right now, the issue is that mainLine-> seems not to affect any change to mainLine. I am not sure why that might be happening. Please let me know if more information is required.wrote on 16 Nov 2022, 11:41 last edited by JonB@Dummie1138 said in QFrame is unused:
I have a warning that mainLine is not used
That would be very surprising given the code you show. Can you show this warning being issued and the code it is against to verify what you say? For example, maybe if you have a member variable also named
mainLine
it might report if that is never used (I don't know whether it does), but I don't see how it would from the extract you actually show.... -
@Dummie1138 said in QFrame is unused:
I have a warning that mainLine is not used
That would be very surprising given the code you show. Can you show this warning being issued and the code it is against to verify what you say? For example, maybe if you have a member variable also named
mainLine
it might report if that is never used (I don't know whether it does), but I don't see how it would from the extract you actually show....wrote on 16 Nov 2022, 12:13 last edited by Dummie1138@JonB @jsulm Here is more code, thanks for your patience and time.
NewConfig::NewConfig(QWidget *parent) : QDialog(parent), ui(new Ui::NewConfig) { setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint); ui->setupUi(this); this->move(0, 0); //Build the main line. QFrame *mainLine = new QFrame(this); QRect lineRect = QRect(20, 160, 400, 40); mainLine->setFrameRect(*lineRect); //mainLine->setGeometry(20, 160, 400, 40); //This was added based on jsulm's suggestions. I've disabled it for this post. mainLine->setLineWidth(5); mainLine->setFrameStyle(QFrame::Raised); qDebug() << lineRect; qDebug() << mainLine->frameRect(); mainLine->show(); }
The output is:
QRect(20,160 400x40) QRect(0,0 400x40)
I have also restarted Qt Creator and noticed the warning having disappeared. Again, thank you for your time and patience, and please let me know if more information is required.
-
@JonB @jsulm Here is more code, thanks for your patience and time.
NewConfig::NewConfig(QWidget *parent) : QDialog(parent), ui(new Ui::NewConfig) { setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint); ui->setupUi(this); this->move(0, 0); //Build the main line. QFrame *mainLine = new QFrame(this); QRect lineRect = QRect(20, 160, 400, 40); mainLine->setFrameRect(*lineRect); //mainLine->setGeometry(20, 160, 400, 40); //This was added based on jsulm's suggestions. I've disabled it for this post. mainLine->setLineWidth(5); mainLine->setFrameStyle(QFrame::Raised); qDebug() << lineRect; qDebug() << mainLine->frameRect(); mainLine->show(); }
The output is:
QRect(20,160 400x40) QRect(0,0 400x40)
I have also restarted Qt Creator and noticed the warning having disappeared. Again, thank you for your time and patience, and please let me know if more information is required.
wrote on 16 Nov 2022, 12:19 last edited by JonB@Dummie1138 said in QFrame is unused:
QRect lineRect = QRect(20, 160, 400, 40); mainLine->setFrameRect(*lineRect);
For a start the above will not compile....
mainLine->show();
You are attempting to show a
QFrame
here, but you are inside the constructor of aQDialog
(which itself is not shown at this point). Widgets should not beshow()
n in widget constructors. Where do you actually show the dialog?You have just added:
The output is:
How do you get output from code which as shown will not compile?
-
@Dummie1138 said in QFrame is unused:
QRect lineRect = QRect(20, 160, 400, 40); mainLine->setFrameRect(*lineRect);
For a start the above will not compile....
mainLine->show();
You are attempting to show a
QFrame
here, but you are inside the constructor of aQDialog
(which itself is not shown at this point). Widgets should not beshow()
n in widget constructors. Where do you actually show the dialog?You have just added:
The output is:
How do you get output from code which as shown will not compile?
wrote on 16 Nov 2022, 12:22 last edited by Dummie1138@JonB Sorry, that *lineRect is a mistype. The proper one is just "lineRect".
My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.
-
@JonB Sorry, that *lineRect is a mistype. The proper one is just "lineRect".
My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.
wrote on 16 Nov 2022, 12:24 last edited by JonB@Dummie1138
Since you are typing your code in and not pasting it, I will leave it to others answer. Too many years of replying to people who do not paste their code and after we all work on it discover it isn't actually what their code reads.... -
@Dummie1138
Since you are typing your code in and not pasting it, I will leave it to others answer. Too many years of replying to people who do not paste their code and after we all work on it discover it isn't actually what their code reads....wrote on 16 Nov 2022, 12:27 last edited by@JonB I see. Again, thank you for your time and patience.
-
@JonB I see. Again, thank you for your time and patience.
wrote on 16 Nov 2022, 12:48 last edited by@Dummie1138
You have responded politely, and I accept that :) Honestly, it's really important to actually copy & paste code when you want help, you are not the first person to have made a mistake when typing in instead, it's too unreliable!I don't understand what it is that you do or do not see when/where?
My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.
Are you expecting to see the
QFrame
visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?I have not used
QFrame
. If @jsulm says aboveI suggested to you to use setGeometry() in your other thread...
I would be looking into that if I were you :)
-
@Dummie1138
You have responded politely, and I accept that :) Honestly, it's really important to actually copy & paste code when you want help, you are not the first person to have made a mistake when typing in instead, it's too unreliable!I don't understand what it is that you do or do not see when/where?
My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.
Are you expecting to see the
QFrame
visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?I have not used
QFrame
. If @jsulm says aboveI suggested to you to use setGeometry() in your other thread...
I would be looking into that if I were you :)
wrote on 16 Nov 2022, 13:36 last edited by@JonB I will make sure to copy code instead of typing it, thanks for the advice.
Are you expecting to see the QFrame visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?
I know the QFrame is run-time code, my issue is that the QFrame is not visible during run-time.I have modified my code to this following, switching to setGeometry.
QFrame *mainLine = new QFrame(this, Qt::Widget); mainLine->setGeometry(20, 160, 400, 40); mainLine->setLineWidth(5); mainLine->setFrameStyle(QFrame::Raised); qDebug() << mainLine->frameRect(); mainLine->show();
This is the location on my ui where the QFrame is supposed to be:
(Sadly I cannot show the entire thing. I hope this remains somewhat helpful.)And this is what my output looks like.
QRect(0,0 400x40)
Again, please let me know if more information is required.
-
@JonB I will make sure to copy code instead of typing it, thanks for the advice.
Are you expecting to see the QFrame visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?
I know the QFrame is run-time code, my issue is that the QFrame is not visible during run-time.I have modified my code to this following, switching to setGeometry.
QFrame *mainLine = new QFrame(this, Qt::Widget); mainLine->setGeometry(20, 160, 400, 40); mainLine->setLineWidth(5); mainLine->setFrameStyle(QFrame::Raised); qDebug() << mainLine->frameRect(); mainLine->show();
This is the location on my ui where the QFrame is supposed to be:
(Sadly I cannot show the entire thing. I hope this remains somewhat helpful.)And this is what my output looks like.
QRect(0,0 400x40)
Again, please let me know if more information is required.
wrote on 16 Nov 2022, 14:16 last edited byThis issue has been resolved.
By adding this line to the QFrame, the QFrame was finally displayed.
mainLine->setParent(this);
The reason for the output of
qDebug << mainLine->frameRect();
placing the location of the Rect at 0,0 is not due to a lack of response but because the Rect is always parented to the QFrame, which means it's location relative to the QFrame is always 0,0.
-
This issue has been resolved.
By adding this line to the QFrame, the QFrame was finally displayed.
mainLine->setParent(this);
The reason for the output of
qDebug << mainLine->frameRect();
placing the location of the Rect at 0,0 is not due to a lack of response but because the Rect is always parented to the QFrame, which means it's location relative to the QFrame is always 0,0.
wrote on 16 Nov 2022, 14:28 last edited by JonB@Dummie1138 said in QFrame does not display in UI properly:
mainLine->setParent(this);
Well, I truly do not see how this could make any difference! I have tried it in my copy of your code I am playing with and it certainly makes no difference. Since you already had
QFrame *mainLine = new QFrame(this)
that has already done themainLine->setParent(this);
you claim makes it work!Would you care to show your complete, working code for this frame/dialog? :)
-
@Dummie1138 said in QFrame does not display in UI properly:
mainLine->setParent(this);
Well, I truly do not see how this could make any difference! I have tried it in my copy of your code I am playing with and it certainly makes no difference. Since you already had
QFrame *mainLine = new QFrame(this)
that has already done themainLine->setParent(this);
you claim makes it work!Would you care to show your complete, working code for this frame/dialog? :)
wrote on 16 Nov 2022, 14:43 last edited by Dummie1138@JonB And now it doesn't work again.
Current code, as much as I can show:
NewConfig::NewConfig(QWidget *parent) : QDialog(parent), ui(new Ui::NewConfig) { setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint); ui->setupUi(this); this->move(0, 0); //Build the main line. QFrame *frontLine = new QFrame(this); frontLine->setGeometry(0, 0, 400, 40); frontLine->setLineWidth(10); frontLine->setFrameStyle(QFrame::Raised); frontLine->setParent(this); //I was working under the impression that the (this) at QFrame initialization is the same as setParent(this). frontLine->show(); QLabel *labelOne = new QLabel("REEEE", this); labelOne->move(200, 200); }
Edit:
frontLine->setStyleSheet("border: 10px solid #0000FF;")
was added to the QFrame code. It now displays. Quite the silly oversight on my behalf.
-
@JonB And now it doesn't work again.
Current code, as much as I can show:
NewConfig::NewConfig(QWidget *parent) : QDialog(parent), ui(new Ui::NewConfig) { setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint); ui->setupUi(this); this->move(0, 0); //Build the main line. QFrame *frontLine = new QFrame(this); frontLine->setGeometry(0, 0, 400, 40); frontLine->setLineWidth(10); frontLine->setFrameStyle(QFrame::Raised); frontLine->setParent(this); //I was working under the impression that the (this) at QFrame initialization is the same as setParent(this). frontLine->show(); QLabel *labelOne = new QLabel("REEEE", this); labelOne->move(200, 200); }
Edit:
frontLine->setStyleSheet("border: 10px solid #0000FF;")
was added to the QFrame code. It now displays. Quite the silly oversight on my behalf.
wrote on 16 Nov 2022, 15:00 last edited by JonB@Dummie1138 said in QFrame does not display in UI properly:
frontLine->setParent(this); //I was working under the impression that the (this) at QFrame initialization is the same as setParent(this).
Indeed, as I said that was already the case! As can be verified by preceding this line with:
Q_ASSERT(frontLine->parent() == this);
So that
setParent()
can be removed!Nor is the
frontLine->show();
needed, and I would not put it into this constructor personally.frontLine->setStyleSheet("border: 10px solid #0000FF;")
Yep, that made all the difference to visibility! :)
1/14