QStackedWidget connect QActions to custom QGraphicsView of QWidget - qt

I've made a MainWindow with a QStackedWidget and a QMenuBar.
One of my widgets contains a QGraphicsView that I promoted to my custom QGraphicsView (DrawingView).
I'd like to connect the QActions from the menuBar to my custom DrawingView.
I managed to get the QActions that were connected to the widget working, but I don't know how to access the methods from the DrawingView.
This is the code I used for the other QActions:
draw = qobject_cast<Drawing*>(ui->stackedWidget->widget(1));
connect(ui->actionOpen, &QAction::triggered, draw, &Drawing::openPhoto);
connect(ui->actionSave, &QAction::triggered, draw, &Drawing::saveFile);
connect(ui->actionExit, &QAction::triggered, draw, &Drawing::close);
I tried this for the DrawingView, but I know that it just makes a new DrawingView instead of using the one from the Draw ui.
drawView = new DrawingView();
connect(ui->actionZoom_In, &QAction::triggered, drawView, &DrawingView::zoomIn);
connect(ui->actionZoom_Out, &QAction::triggered, drawView, &DrawingView::zoomOut);
I also tried this, which gave an error on ui->stackedWidget->widget(1)->graphicsView:
drawView = qobject_cast<DrawingView*>(ui->stackedWidget->widget(1)->graphicsView); //also tried (ui->stackedWidget->widget(1)->ui->graphicsView)
connect(ui->actionZoom_In, &QAction::triggered, drawView, &DrawingView::zoomIn);
connect(ui->actionZoom_Out, &QAction::triggered, drawView, &DrawingView::zoomOut);
Any help on how to connect from the MainWindow or how to access the ui of MainWindow inside 2nd widget.

Isn’t your problem simply that graphicsView is a private member of the containing widget?
You could either make it a public member or add a wrapper function:
auto draw = qobject_cast<Drawing*>(ui->stackedWidget->widget(1));
connect(ui->actionZoom_In, &QAction::triggered, draw, &Drawing::zomIn);
and in the Drawing class:
void Drawing::zoomIn() {
ui->graphicsView.zoomIn();
}
A third option is to
emit a custom zoomIn signal from the MainWindow
make the main window accessible to Drawing (e.g. passing it to the constructor or creating a global mainWindow singleton)
and do the connect directly in Drawing

Related

How to access a QAction using the QtTest lib?

I have a pop-up menu in a QTableWidget (resultTable). In the constructor of my class I set the context menu policy:
resultTable->setContextMenuPolicy(Qt::CustomContextMenu);
connect(resultTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(popUpMenuResultTable(QPoint)));
The popUpMenuResultTable function:
void MyClass::popUpMenuResultTable(QPoint pos)
{
QMenu menu;
QAction* actionExport = menu.addAction(QIcon(":/new/prefix1/FileIcon.png"), tr("Export"));
connect(actionExport, SIGNAL(triggered()), this, SLOT(exportResultsTable()));
menu.popup(pos);
menu.exec(QCursor::pos());
}
Now, I need to implement a function to test my GUI using the QtTest lib.
How can I produce the same result as a user by right clicking on my resultTable? Basically, I need to get access to the actionExport (QAction) and trigger it.
For example:
I already tried:
QTest::mouseClick(resultTable, Qt::RightButton, Qt::NoModifier, pos, delay);
but it does not show the QMenu.
I'm using Qt 5.3.2.
Maybe not entirely what you are after but an alternative approach that is easier to test.
Instead of creating the menu manually you register the actions with the widgets and use Qt::ActionContextMenu:
// e.g. in the widget's constructor
resultTable->setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* actionExport = menu.addAction(QIcon(":/new/prefix1/FileIcon.png"), tr("Export"));
connect(actionExport, SIGNAL(triggered()), this, SLOT(exportResultsTable()));
resultTable->addAction(actionExport);
Then you either add an accessor to your widget that returns resultTable->actions() or just make actionExport a member of your class.
Once your test code has access to the action it can simply call its trigger trigger() method.

Show a QWidget after pressing a button

I want to change a QWidget in a QMainWindow dynamically. Therefore, the old widget (if it exists) will be deleted, a new one will be constructed and added to the main window.
The widget (_visualization) is a QMainWindow itself that contains a menu bar and a widget that shows an OSG scene graph.
If I don´t call show() on the new widget, I will only see the menu bar, but not the scene graph.
My goal is to call show(), when the user clicks on a button. The button is connected with the outer QMainWindow (MyQMainWindow).
Unfortunately, when I call show() on _visualization in the connected method, the scene graph will not be shown. In contrast to that, the scene graph will be shown, if I call it in the constructor method (loadVisualization(...)).
MyQMainWindow::MyQMainWindow(QWidget *parent ) :
QMainWindow(parent) {
...
loadVisualization(...);
connect(_ui->nextButton, SIGNAL(clicked()), this, SLOT(showNext()));
...
}
void MyQMainWindow::loadVisualization(QString filePath) {
if (_visualization) {
_heightWidgetLayout->removeWidget(_visualization);
delete _visualization;
}
_visualization= new HeightVisualization(0, filePath);
_visualization->setParent(_mainWindowWidget);
_heightWidgetLayout->addWidget(_visualization);
//_visualization->show(); // will work here
}
void MyQMainWindow::showNext() {
_visualization->show(); // does not work here!
}
I know that QT will call setVisible(...) on the widget. This method first tests some states in QT (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden)). If I call show() in showNext(), these tests lead to a return without any change.
Is it a problem with connectors and slots? Is there a possibility to show the widget, when the user clicked on a button?
What I have learned is that it is easy to use stackedWidget.
You can then programmatically change it with this
currentPageIndex = (currentPageIndex + 1) % 3;
ui->stackedWidget->setCurrentIndex(0);
The 3 can be the total pages you have in your stack widget. You can either use currentPageIndex, or you can just create some constants with the page ids like I have done with the 0.

QGraphicsView->centerOn() doesn't work

I can't figure out how to make centerOn() to move viewport around the item (or the item around the port, not sure which way it is).
The following code does work in main:
view.setDragMode(QGraphicsView::ScrollHandDrag);
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setFrameStyle(QFrame::NoFrame);
view.showFullScreen();
QGraphicsPixmapItem *pmItem = new QGraphicsPixmapItem(pixMap);
scene.addItem(pmItem);
view.centerOn(QPointF(50,30));
view.show();
rc = qA.exec();
However, the centerOn() does nothing when I am trying to do the same from a paintEvent() of an overloaded QGraphicsView:
class MyView :: public QGraphicsView { ... }
void MyView::init(){
...
setDragMode(QGraphicsView::ScrollHandDrag);
setFrameStyle(QFrame::NoFrame);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_scene.clear();
setScene(&_scene);
super::showNormal();
}
void MyView::paintEvent(QPaintEvent *aEvent){
_scene.clear();
QGraphicsPixmapItem *pmItem = new QGraphicsPixmapItem(pixMap);
_scene.addItem(pmItem);
centerOn(QPointF(50.0, 30.0));
super::paintEvent(aEvent);
}
What do I miss?
Thank you
Alex
You basically pull the rug under the paintEvent call.
QGraphicsView communicates with QGraphicsScene by way of signals and slots. The scene has no chance to notify the view that a new item is just added to it since the actual paintEvent is called right after the creation. There's no event loop process between the 2 steps. So the paintEvent call doesn't have it setup properly for the new item, let alone the centering change.
And once the painting is finished, the view is validated and thinks it's all painted. So the it'll never update the area of the new item.
Call centerOn outside of any painting or updating event.
I solved this problem by placing a high value on setSceneRect.
Then I centralize the scene on an object or position.
example:
this->ui->graphicsView->setSceneRect (0,0,100000000, 100000000);
this->ui->graphicsView->centerOn(500,1030);
With the setSceneRect size GraphicsView does not work right.

Showing a popup menu on QGraphicsScene click or right click

Is there a way to show a popup when the user right clicks on an empty portion of the scene?
I'm new at Qt and I've tried slots and subclassing, but to no avail.
No such slot and, respectively:
"error: 'QMouseEvent' has not been declared"
when trying to implement the onMouseRelease event.
QGraphicsView is the widget used for displaying the contents of the QGraphicsScene. So the correct place to implement the context menu (popup menu) is the QGraphicsView.
You need to reimplement the contextMenuEvent function is your own class inherited from QGraphicsView:
void YourGraphicsView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(...);
menu.addAction(...);
...
menu.exec(event->globalPos());
}
See also the Qt's Menus Example.
You can re-implement the contextMenuEvent method of the QGraphicsScene class, which will give you access to the scene coordinates as well as the screen coordinates (as opposed to QGraphicsView, which also works but doesn't have this information):
void YourGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
// event->scenePos() is available
QMenu menu(this);
menu.addAction(...);
menu.addAction(...);
...
menu.exec(event->screenPos());
}

How can I add another tab that looks exactly like the first one (like in a browser)?

I have a browser made in Qt and a I have a tabwidget with one tab (which has a label, lineedit and a webview). I want to add others that look like the first one (have label, lineedit and webview).
How can I do this?
I don't know of any way to "clone" or duplicate an existing tab or widget, so I believe you'll need to code the tab contents yourself (i.e. not through the designer).
If all you need are a QLabel, a QLineEdit and a QWebView, that's not very complex. The idea would be to:
create a custom widget (inheriting from QWidget directly, or from QFrame)
lay out the contained widgets in the fashion you want in its constructor
add as many tabs as you want, when you want them, via the QTabWidget.addTab function.
The Tab Dialog example has everything you need - it's actually more complex than what you need because it uses different widgets for each tab. You can get away with a single widget.
If you wonder how to do the layout, and you're satisfied with what you got from the designer, you can inspect the generated (.moc) files. You'll see what layouts it uses, and you can replicate that in your own code.
Skeleton widget:
class BrowserTab : public QWidet
{
Q_OBJECT
public:
BrowserTab(QUrl const& home, QWidget *parent = 0);
void setUrl(QUrl const& url);
private:
QWebView *web;
QLabel *title;
QLineEdit *urlEdit;
};
BrowserTab::BrowserTab(QUrl const& home, QWidget *parent)
: QWidget(parent)
{
urlEdit = new QLineEdit(this);
title = new QLabel(this);
web = new QWebView(this);
QVBoxLayout *vl = new QVBoxLayout;
vl->addLayout(title);
vl->addLayout(urlEdit);
vl->addLayout(web);
setLayout(vl);
setUrl(home);
}
void BrowserTab::setUrl(QUrl const& url)
{
web->load(url);
// update label & urlEdit here
}
You'll need to do a bit more to make it a proper browser (setUrl should probably be a slot too), but this should get you started.

Resources