How to implement Menu bar inside QGraphicsView? - qt

I have a QGraphicsView which shows images dynamically. I also used fitInView feature to resize the window. Now I need to add the menu bar at the top of the QGraphicsView. How to implement this? Please help. I am new in Qt

As beginner it is probably easiest to place your QGraphicsView into a QVBoxLayout and create a QMenuBar and insert it above your graphics view.
QMenuBar *bar = new QMenuBar();
ui->yourVerticalLayout->insertWidget( 0, bar );
QMenu* yourMenu = bar->addMenu("Your Menu title");
QAction* yourFirstAction = yourMenu->addAction("Your First Action");

Related

PushButtons on QGraphicsView aren't clickable

I want to overlay a QGLWidget with clickable buttons.
I'm using a QGraphicsView and setting the viewport to a QGLWidget. Then I'm adding a layout to the QGraphicsView, and adding several QPushButtons to that.
The Buttons display correctly, HOWEVER they are not clickable.
Here is my code:
QGraphicsView* view = ui->gView;
QGLWidget* gl = new QGLWidget(QGLFormat(QGL::SampleBuffers));
view->setViewport(gl);
QPushButton* push = new QPushButton("Click me");
QHBoxLayout* h = new QHBoxLayout;
QPushButton* wo = new QPushButton("Wo");
h->addWidget(wo);
h->addWidget(push);
view->setLayout(h);
Is there a simple way I can make the buttons get the clicks when the mouse is above them, and QGLWidget get the clicks otherwise?

Adding QGraphicsView and QGraphicsScene to a label in Qt

I have a stacked widget on which I have a QLabel. I display an image on this label. I want to zoom and pan this image and I am trying to use the QGraphicsView technique. However a new window opens.
This is what I am doing.
scene = new QGraphicsScene(this);
view = new QGraphicsView(this);
QPixmap pix("/root/Image);
label->setPixmap(pix);
scene->addWidget(label);
view->setScene(scene);
view->setDragMode(QGraphicsView::scrollHandDrag);
view->show();
Can someone please suggest me what I should do. I want the label to act like the QGraphicsView.
Thank You :)
You create a scene and view and add the label to the scene, then tell the view to show itself with: -
view->show()
If, as you state, you want the QGraphicsView on the label, don't add the label to the scene, but add the QGraphicsView to the label: -
QLabel* pLabel = new QLabel(mainWindow); // setting mainWindow as the parent
QGraphicsView* pView = new QGraphicsView(pLabel) // set the label as the parent of the view.
You do not need to call show, as the label will handle that for you, assuming the label is on a Widget that is already displayed.
Now, instead of setting the pixmap on the label, create a pixmap in the scene with: -
pScene->addPixmap(pixmap);

Prevent widgets stretching in QVBoxLayout and have scrollbar appear in QScrollArea?

Making my way up the Qt learning curve, I've seen many questions about dynamic layouts but the solutions aren't working for me or I don't quite understand them.
Reference questions:: Qt Scroll Area does not add in scroll bars, How can i make widgets overflow to make a scrollbar appear in Qt?
Question:: I want to have a dynamic layout of a set of widgets within a QScrollArea. I've been able to do this manually in Qt Creator and now I am trying to do it through code.
How do I prevent the widgets from stretching/force the area to scroll?
How do I have the added widgets start from the top? I have a vertical spacer in my QVBoxLayout but that pushes everything to the bottom.
Simple test code::
void MainWindow::on_pushButton_clicked()
{
ui->myScroll->setWidgetResizable(true); //making sure this is set
QPushButton *b = new QPushButton(this);
b->setText(QString("Hello Button"));
QHBoxLayout *h = new QHBoxLayout();
h->addWidget(b,0);
ui->myVBoxLayout->addLayout(h,0);
}
Result:: Left side squished (dynamic) – Right side Ok (set up manually)
Qt Creator Setup:: Left side: dynamic – Right side set up manually
Properties::
You can set use setMinimumHeight() on your buttons for preventing squished buttons. The layout can be configured with setContentsMargin() for space between item-border and item-content (QtDesigner has all four directions set to 9 IIRC) and setSpacing() for space between items (QtDesigner uses a default of 6). Also setWidgetResizable(true) allows your scrollarea to resize the view widgeth inside the area (this is where your layout and children are being placed).
This works for me:
In constructor or code set scrollArea->widget() to hold the QVBoxLayout:
v = new QVBoxLayout;
ui->scrollArea->widget()->setLayout(v);
In Button Slot:
void MainWindow::pushButtonPressed()
{
ui->scrollArea->setWidgetResizable(true);
QPushButton *b = new QPushButton(this);
b->setText(QString("Hello Button"));
QHBoxLayout *h = new QHBoxLayout();
h->addWidget(b,0);
v->addLayout(h);
}

How to Add QListView/QListWidget to QGraphicsScene in Qt?

How to Add QListView/QListWidget to QGraphicsScene and add Widgets to ListView
When i Try to add QLisView to QGraphicsScene mouse scroll affects goes from Scene.
I want to add QPushButtons as ListView Items in QgraphicsScene with mouse scroll affect.
Thanks.
What about QGraphicsProxyWidget?
QListView *listView = new QListView;
QGraphicsProxyWidget *proxy = scene.addWidget(listView);
Then (or before that) you can populate the list with anything you want. QPushButton can be added to the list using setIndexWidget(). Also you might rethink the whole idea of having a QListView, and give it a try with QScrollArea and a linear layout containing buttons. That would require a bit more logic to organize items within the scroll area, but it should be more lightweight that QListView with custom widgets.
I second the answer above: ProxyWidget is the answer.
Here is my working code,
Header:
class CenterScreen{
private:
QListWidget* nameListWidget;
QGraphicsProxyWidget* nameProxyWidget;
...
C++ source:
void CenterScreen::addListView()
{
QGraphicsScene* scene = ui.centerGraphicsView->scene();
nameListWidget = new QListWidget();
nameProxyWidget = scene->addWidget(nameListWidget);
...
nameProxyWidget->hide(); // you can control your widget as you like

How to add a scrollbar to parent QWidget

I understand how to add a scrollArea to a particular widget. However in my case Qwidget has multiple child widgets and these are all set using QVBoxLayout. Now how can I add a scroll bar in this case? Here QWidget is not the center widget, its one of the pages of the TabWidget. My code looks like:
QTabWIdget *center = new QTabWidget; setCentralWIdget(center);
xTab = new QWidget;
formLayout = new QFormLayout; formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...);
xTab->setLayout(formLayout);
Now how can I set the scrollBar to xTab? I tried using
scrollArea = new QScrollArea;
scrollArea->setWidget(xTab);
however, this isn't working.
Any idea/suggestions are helpful and appreciated.
Have you tried using QScrollArea as the tab page?
QTabWIdget *center = new QTabWidget; setCentralWIdget(center);
xTab = new QScrollArea;
formLayout = new QFormLayout; formLay....
xTab->setLayout(formLayout);
center->addTab(xTab, "XXX Tab");
I had success using the following:
scroll=new QScrollArea(mainWindow->centralWidget);
scroll->setGeometry(mainWindow->tabWidget->geometry());
scroll->setWidget(mainWindow->tabWidget);
scroll->show();
The QScrollArea defines where the scrollable widget will appear. If parent is 0, it's a non-modal window. setGeometry sets the QScrollArea instance to the desired dimensions (that of the tab). setWidget defines what widget the QScrollArea will actually be scrolling.

Resources