QT Display a QWidget above QGraphicsScene - c++

Currently I have a QGraphicsScene that is put inside a QGraphicsView and is shown on the display. I add all my elements to my scene that I set as the active scene.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView w;
GameScene *gameScene = new GameScene(); // GameScene extends QGraphicsScene, adds tons of elements to the scene
w.setScene(gameScene);
w.show();
return a.exec();
}
Above this scene I want a bar that contains several layout elements, like several QProgressBar.
For what I have found so far, QWidget's can be positioned easily. I've made already a widget of what I need to be displayed above the scene:
QWidget *dummyWidget = new QWidget();
QFormLayout *formLayout = new QFormLayout;
QProgressBar *bar1 = new QProgressBar();
QProgressBar *bar2 = new QProgressBar();
bar1->setValue(20);
bar2->setValue(100);
formLayout->addRow("&Health:", bar1);
formLayout->addRow("&Energy:", bar2);
dummyWidget->setLayout(formLayout);
dummyWidget->show();
But how do I get this to be displayed above my QGraphicsScene?

If you want to display your widget above the view you can have a layout similar to the one for dummyWidget and add the widget and view in it :
QGraphicsView w;
QWidget *widget = new QWidget();
QFormLayout *formLayout2 = new QFormLayout(widget);
QWidget *dummyWidget = new QWidget();
QFormLayout *formLayout = new QFormLayout;
QProgressBar *bar1 = new QProgressBar();
QProgressBar *bar2 = new QProgressBar();
bar1->setValue(20);
bar2->setValue(100);
formLayout->addRow("&Health:", bar1);
formLayout->addRow("&Energy:", bar2);
dummyWidget->setLayout(formLayout);
formLayout2->addRow("", dynamic_cast<QWidget*>(dummyWidget));
formLayout2->addRow("", dynamic_cast<QWidget*>(&w));
widget->show();
If you want to add the widget in the scene, you can use QGraphicsScene::addWidget which creates a new QGraphicsProxyWidget for widget, adds it to the scene, and returns a pointer to the proxy :
QGraphicsProxyWidget * item = gameScene->addWidget(dummyWidget);
item->setPos(100,100);
item->setZValue(1);
You can also add it to an item :
item->setParentItem(anOtherItem);

Related

Qt Application with layout's, QPushButton and QGraphicsItem

I am trying to draw various shapes like rectangle, ellipse, text etc uisng QGraphicsView and QGraphicsScene. For that I am trying to create an interface where there will be a vertical layout and besides that there will be few buttons. On clicking those buttons, I can show various QGraphicsItem's on screen. I want to create this interface programatically but not using ui.
I tried and ended up this way.
I wanted buttons on the right side and verticalLayout on the left side and both should filled up the whole screen.
Here is my current implementation :
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsView* view = new QGraphicsView(this);
view->setScene(scene);
QWidget* mainWidget = new QWidget(this);
QHBoxLayout *mainLayout = new QHBoxLayout();
QVBoxLayout *buttonLayout = new QVBoxLayout();
QVBoxLayout *vlayout2 = new QVBoxLayout();
vlayout2->addWidget(view);
QPushButton *btn1 = new QPushButton("Rectangle");
btn1->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
QPushButton *btn2 = new QPushButton("Ellipse");
btn2->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
QPushButton *btn3 = new QPushButton("Text");
btn3->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
buttonLayout->addWidget(btn1);
buttonLayout->addWidget(btn2);
buttonLayout->addWidget(btn3);
buttonLayout->addStretch();
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(vlayout2);
mainWidget->setLayout(mainLayout);
}
Can anyone guide me ?
Actually, it should work with the hints given in my comments.
I made an MCVE to convince myself:
#include <QtWidgets>
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
QWidget qMain;
qMain.setWindowTitle("Test Box Layout");
qMain.resize(640, 320);
QHBoxLayout qHBox;
QVBoxLayout qVBoxBtns;
QPushButton qBtnRect("Rectangle");
qVBoxBtns.addWidget(&qBtnRect);
QPushButton qBtnCirc("Circle");
qVBoxBtns.addWidget(&qBtnCirc);
QPushButton qBtnText("Text");
qVBoxBtns.addWidget(&qBtnText);
qVBoxBtns.addStretch();
qHBox.addLayout(&qVBoxBtns);
QVBoxLayout qVBoxView;
QGraphicsView qView;
qVBoxView.addWidget(&qView, 1);
qHBox.addLayout(&qVBoxView, 1);
qMain.setLayout(&qHBox);
qMain.show();
// runtime loop
return app.exec();
}
Output:
Thus, there must be something else in OP's code…
Unfortunately, OP didn't expose an MCVE.
Thus, it's not clear how OP's Widget is instanced. Is it the widget which becomes the main window? Is there another widget where the Widget's instance becomes child of?
It's just guessing but the latter would explain what OP described.
To confirm my guess, I modified the above code a bit:
// setup GUI
QWidget qMain0; // main window widget
QWidget qMain(&qMain0); // child widget of main window widget
⋮
qMain.setLayout(&qHBox);
qMain0.show();
// runtime loop
return app.exec();
Please, note that qMain is now a child of qMain0 but there is no layout which adjusts the size of qMain when qMain0 is resized.
Hence, the size of view stays the initial size while the window is resized.

I want to add a label in new widget using the Qt framework

This is my code :
void maquette::on_btn_edit_clicked()
{
QWidget* wdg = new QWidget;
wdg->resize(320, 340);
wdg->setWindowTitle("Modiffier");
QLabel label1("matricule", wdg);
label1.setGeometry(100, 100, 100, 100);
wdg->show();
}
the window shows up but the label didn't show
void maquette::on_btn_edit_clicked()
{
QWidget *wdg = new QWidget;
wdg->resize(320,340);
wdg->setWindowTitle("Modiffier");
QLabel *label1 = new QLabel("matricule",wdg);
label1->setGeometry(100, 100, 100, 100);
wdg->show();
}
You can either add the QLabel using parenting. as mentioned before.
QLabel *label1 = new QLabel("matricule",wdg);
or
QLabel *label1 = new QLabel("matricule");
label1->setParent(wdg);
This will make the widget float inside its parent.
You can also add the QLabel to a layout that has been assigned to the QWidget.
QVBoxLayout* layout = new QVBoxLayout();
wdg->setLayout(layout);
QLabel *label1 = new QLabel("matricule");
layout->addWidget(label1);
This will add the widget to the layout.
The layout will control how the child widgets are laid out.

Qt:Unable to directly add scrollbar to a widget with children

Im new to Qt programming and I want to add a scrollbar to a widget which is having child widgets within it.I have seen several questions/posts about this like:
1.How to add a scrollbar to parent QWidget
2.Insert a scrollbar in a qt widget using qtcreator
3.Adding scroll bar to a Qwidget
4.QScrollArea missing Scrollbar
But most of the answers set a layout to the widget for which we add the scrollbar.
My Problem:
The widget for which I need scrollbar has many child widgets within it.But I haven't added any layout to it.The geometry of the child widgets are modifiable and so I haven't added any layout to the parent widget.
Below is my code:
class Absolute : public QWidget {
public:
Absolute(QWidget *parent = 0);
};
Absolute::Absolute(QWidget *parent)
: QWidget(parent) {
QTextEdit *ledit = new QTextEdit(this);
ledit->setGeometry(5, 5, 500, 550);
QTextEdit *lledit = new QTextEdit(this);
lledit->setGeometry(510, 5, 250, 550);
/*QScrollArea* sa = new QScrollArea();
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
auto *widget = new QWidget(this);
sa->setWidget(widget);
auto *l = new QVBoxLayout(this);
l->setMargin(0);
l->addWidget(sa);*/
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Absolute window;
window.setWindowTitle("Absolute");
window.setGeometry(500,500,1500,1000);
window.show();
return app.exec();
}
However without the scrollbar code(commented portion),the UI has those textedits in the given position as set in the setGeometry.
All I need is to bring a scrollbar if the 2nd textedits width is more.
So I tried adding the scrollbar(the commented portion).However I can see only the scrollbar and not the textedits.
Any suggestion/inputs will be really helpful.Thanks in advance!!
Cause
The way you set the parents when you create the widgets and layouts is not correct.
Solution
Create the correct parent/child hierarchy and set the desired size of the QScrollArea's widget. There is no need to set a layout to this widget.
Example
Here is an example I have prepared for you in order to demonstrate how you could fix Absolute:
class Absolute : public QWidget {
public:
Absolute::Absolute(QWidget *parent = nullptr)
: QWidget(parent)
{
auto *sa = new QScrollArea(this);
auto *l = new QVBoxLayout(this);
auto *widget = new QWidget();
auto *ledit = new QTextEdit(widget);
auto *lledit = new QTextEdit(widget);
sa->setWidgetResizable(true);
sa->setWidget(widget);
sa->setAlignment(Qt::AlignLeft | Qt::AlignTop);
ledit->setGeometry(5, 5, 500, 550);
lledit->setGeometry(510, 5, 250, 550);
widget->setFixedSize(lledit->geometry().right(), lledit->geometry().bottom());
l->setMargin(0);
l->addWidget(sa);
}
};
Note: For demonstration purposes the size of widget is set to (lledit->geometry().right(), lledit->geometry().bottom()). You might consider adjusting it according to your specific needs.

QGraphicsScene many widgets

I have more than one widget in QGraphicsScene. The first created widget is on top of scene, other widgets is under first widget. I want such functionality - if i press one of widget it will be on top of the scene. How i can make it?
Also i don't know, how to make border to the widget in QGraphicsScene like the normal widget. This is the code:
QGraphicsScene *scene ;
QGraphicsProxyWidget *pw()
{
QWidget *w = new QWidget;
w->resize(580, 280);
w->setStyleSheet("background-color: rgb(110, 149, 255)");
QPushButton *p = new QPushButton;
p->setParent(w);
p->move(30, 50);
p->setText("hello");
QPushButton *p2 = new QPushButton;
p2->setParent(w);
p2->move(110, 50);
p2->setText("world");
QGraphicsWidget *parentWidget = new QGraphicsWidget();
parentWidget->setMinimumSize(QSizeF(100, 30));
parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
parentWidget->setAutoFillBackground(true);
parentWidget->resize(580, 280);
scene->addItem(parentWidget);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(w);
proxyWidget->setParentItem(parentWidget);
return proxyWidget;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 800, 480);
scene->addItem(pw());
scene->addItem(pw());
scene->addItem(pw());
scene->setBackgroundBrush(Qt::darkGreen);
QGraphicsView view(scene);
view.show();
return a.exec();
}

Qt Framework: How to display a QGraphicsView in a layout?

I am having trouble getting a QGraphicsView to show up in a QVBoxLayout object and I have no idea what is wrong. My code compiles so no errors are being thrown. Here is my simple code. (I am a Qt and C++ newb). At the bottom, I add a QPushButton widget to the layout and that shows up fine. Thanks for your help in advance!
QGraphicsScene scene;
QGraphicsView view(&scene);
view.setBackgroundBrush(QImage(":/images/bg/tile.png"));
view.setCacheMode(QGraphicsView::CacheBackground);
QPixmap pixmap("images/icons/dsp.gif");
QGraphicsPixmapItem* dsp = scene.addPixmap(pixmap);
view.show();
vlayout->addWidget(&view);
vlayout->addWidget(new QPushButton("some button here"));
Not enough context, so I can't tell what's happening exactly. But, if those are in a function, then you are declaring local variables that are gone once the function exits. If you are in the main, you're code should look something like this, but it will probably crash:
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QWidget widget;
view.setBackgroundBrush(Qt::red);
QVBoxLayout vlayout;
widget.setLayout(&vlayout);
vlayout.addWidget(&view);
vlayout.addWidget(new QPushButton("some button here"));
widget.show();
I recommend dynamically allocating objects:
int main(int argc, char* argv[]){
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene;
QGraphicsView* view = new QGraphicsView(scene);
QWidget *widget = new QWidget;
view->setBackgroundBrush(Qt::red);
QVBoxLayout* vlayout = new QVBoxLayout(widget);
vlayout->addWidget(view);
vlayout->addWidget(new QPushButton("some button here"));
widget->show();
return app.exec();
}
Don't forget to delete the parent object so it doesn't leak memory.