I'm trying to perform some animations using a QGraphicsProxyWidget but am not seeing any being applied. For example if I want to just rotate a QGraphicsTextItem this code works:
QGraphicsView *view_ = new QGraphicsView(this);
QGraphicsScene *scene_ = new QGraphicsScene(view);
QGraphicsTextItem *text_item_ = new QGraphicsTextItem("This is some sample text to\ntest if we can rotate the\nimage correctly");
scene_->addItem(text_item_);
text_item_->rotate(180);
view->setScene(scene_);
However this doesn't actually seem to do anything:
QLabel* label = new QLabel(this);
label->setText("This is some sample text to\ntest if we can rotate the\nimage correctly");
QGraphicsView *view_ = new QGraphicsView(this);
QGraphicsScene *scene_ = new QGraphicsScene(view);
QGraphicsProxyWidget *proxy_widget_ = new QGraphicsProxyWidget();
proxy_widget_->setWidget(label);
scene_->addItem(proxy_widget__);
proxy_widget_->rotate(180);
view->setScene(scene_);
Nor does doing it like this:
QGraphicsProxyWidget *proxy_widget_ = scene_->addWidget(label).
Any suggestions?
Your code adds the proxy widget and calls: -
proxy_widget->rotate(180);
If you look at the QGraphicsProxyWidget documentation, you'll see that it inherits from QGraphicsItem and that you should call the function setRotation
proxy_widget->setRotation(180);
Thanks Merlin. Figured out the issue. If you remove the parenting from the QLabel it works.
So change this:
QLabel* label = new QLabel(this);
To this:
QLabel* label = new QLabel();
Related
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.
QGraphicsScene has an addWidget(QWidget *) function, but no corresponding removeWidget(QWidget *). It has only removeItem(QGraphicsItem *).
How do I remove a QWidget?
Here's a basic sample, see if it works for you.
QGraphicsScene scene;
QPushButton *button = new QPushButton;
//Add item and obtain QGraphicsProxyWidget
QGraphicsProxyWidget *proxy = scene.addWidget(button);
//Remove item
scene.removeItem(proxy);
Just remember to delete any memory you allocate.
Edit: After OP's comments, maybe this is what you want
//add item
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
proxy = scene.addWidget(button);
//remove item
scene.removeItem(button->graphicsProxyWidget());
Again, remember to delete proxy, or use a smart pointer.
I have a working solution figured out, but I do have some warnings appearing in the console. Here's a basic approximation of what I'm doing:
QGraphicsScene *scene = new QGraphicsScene();
// Add button
QPushButton button = new QPushButton( "Test", this );
scene->addWidget( button );
// Remove button
QGraphicsProxyWidget *proxy;
proxy = proxy->createProxyForChildWidget( button );
scene_->removeItem( proxy );
delete proxy;
proxy = NULL;
delete button;
button = NULL;
Here are the warnings I see though:
QGraphicsProxyWidget::setWidget: cannot embed widget 0x604a2c8 which is not a toplevel widget, and is not a child of an embedded widget
QGraphicsProxyWidget::createProxyForChildWidget: top-level widget not in a QGraphicsScene
QGraphicsScene::removeItem: cannot remove 0-item
I have QWidget with button. When button is pressed, show new smaller window (Qwidget too). I want then new window is centered horizontal and veritcal on main window. Code which display new window is:
QWidget *wdg = new QWidget;
QPushButton *closeBtn = new QPushButton("Close");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(closeBtn);
wdg->setLayout(layout);
wdg->show();
wdg->resize(400,200);
Use the move slot. For example:
QPoint centerPoint = oldWidget->geometry()->center();
newWidget->adjustSize();
newWidget->move(centerPoint.x() - newWidget->width()/2, centerPoint.y() - newWidget->height()/2);
You may consider using frameGeometry() instead of geometry().
http://qt-project.org/doc/qt-5/application-windows.html#window-geometry
Hope that helps.
Want to place a QPushButton onto a GraphicView... but no button is displayed?
I am creating a new window for each plot. On each plot I want to put some buttons for functionality. I don't know if I should put the view on another type of window... if so how? thx
QGraphicsScene *scene = new QGraphicsScene();
QPixmap image;
image.load(fileInfo.filePath(), 0);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
QGraphicsView *view = new QGraphicsView();
view->setScene(scene);
view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "My Plot"));
view->resize(1000, 1000);
view->show();
QPushButton *m_button6 = new QPushButton("ok", view);
m_button6->setGeometry(QRect(QPoint(50, 50), QSize(50, 50)));
connect(m_button6, SIGNAL(released()), this, SLOT(handleButton5()));
wrap it around QGraphicsWidget.
QGraphicsWidget *pBtn = scene->addWidget(m_button6);
scene->addItem(pBtn)
oh! don't forget:
m_button6->show();
widgets are hidden by default!
I have the following problem with scrollbars in graphics view. My application takes a PDF file and creates (in some way) a QImage out of it. The QImage is then converted to QPixmap, which is used to create a QGraphicsScene and from the QGraphicsScene I create a QGraphicsView. The QGraphicsView is added to the central widget and displayed.
The code looks approximately like this
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
In the application, the view gets updated every time the PDF file changes. Now the problem is that the thing, which is in the PDF file, can also change in size and the scrollbars get messed up. I want the scrollbars after update to be in a position, such that I will see the same part of the PDF file as I saw before the update.
Can you give me some advice on how to accomplish this? I've searched for this issue, but nothing has worked in my case so far (I could have also be doing something wrong).
Thank you for your answers!
Before changing the view's contents remember scrollbar positions using view->horizontalScrollBar()->value() and view->verticalScrollBar()->value().
After changing reset previous values using setValue(int).
It's bad that you didn't provide any code that you have tried to use to accomplish this so I can't tell you what were you doing wrong.
Here is an example:
int pos_x = view->horizontalScrollBar()->value();
int pos_y = view->verticalScrollBar()->value();
pixmap_item->setPixmap(QPixmap::fromImage(new_image));
view->horizontalScrollBar()->setValue(pos_x);
view->verticalScrollBar()->setValue(pos_y);
Here pixmap_item is the stored result of scene->addPixmap(pixmap). It has QGraphicsPixmapItem* type.
I've changed the code, but it behaves strangely.
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
int hsbv = -1;
int vsbv = -1;
if (view != NULL) {
hsbv = view->horizontalScrollBar()->value();
vsbv = view->verticalScrollBar()->value();
}
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
view->horizontalScrollBar()->setVealue(hsbv);
view->verticalScrollBar()->setValue(vsbv);
When I print out the view->horizontalScrollBar()->value() at the end, I always get 83 and I get 479 for the view->verticalScrollBar()->value(), which is very wierd, but when I print hsbv and vsbv I get reasonable numbers.