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.
Related
To my custom widget, inherited from QWidget, I have added a QScrollArea like this:
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)//MainWindow is a QWidget
{
auto *scrollArea = new QScrollArea(this);
auto *widget = new QWidget(this);
widget->setStyleSheet("background-color:green");
scrollArea->setWidget(widget);
scrollArea->setWidgetResizable(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
QVBoxLayout *parentLayout = new QVBoxLayout(widget);
this->setStyleSheet("background-color:blue");
for(int i=0;i<12;i++){
QHBoxLayout* labelLineEdit = f1();
parentLayout->addStretch(1);
parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
echoLineEdit->setMaximumWidth(120);
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:white");
layout->addWidget(echoLineEdit);
layout->setSpacing(0);
return layout;
}
This produces a window which looks like this:
The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.
How could I fix this?
The problem is, that I want the scrollArea to occupy the entire
window, but it does not. It also doesn't get resized when I resize the window.
The reason is that you have not set any kind of layout to manage the positioning of your QScrollArea widget itself, so it is just being left to its own devices (and therefore it just chooses a default size-and-location for itself and stays at that size-and-location).
A simple fix would be to add these lines to the bottom of your MainWindow constructor:
QBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->addWidget(scrollArea);
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();
I have a normal QGraphicsView/QGraphicsScene and all I want to do is load a QPixmap (.png) to the graphic and manually set the position of that QPixmap image. I've found solutions but they don't work on Qt5. Any ideas on how to achieve this on Qt5? Thank you.
QGraphicsScene* scene = new QGraphicsScene(QRect(0, 0, 600, 400));
QPixmap Pix(":/test.gif");
// doesnt work
//Pix.setGeometry(QRect(-30, 40, 260, 200));
QGraphicsPixmapItem *item1 = scene->addPixmap(Pix);
// doesnt work
//item1->setPos(-25, 45);
//scene->addPixmap(Pix)->setPos(0,0);
//QGraphicsView* view = new QGraphicsView(ui->centralWidget);
QGraphicsView* view = new QGraphicsView(this);
view->setScene(scene);
You can browse the image from your computer and set its position.
Just in case, It helps someone.
QString fileNamez = QFileDialog::getOpenFileName(this,"Open Filezzz","C:/");
QGraphicsPixmapItem *pm = scene->addPixmap( QPixmap(fileNamez) );
pm->setPos(xPos,yPos);
If you use a QGraphicsPixmapItem in your implementation, or indeed any Qt object, you need to include it in the cpp: -
#include <QGraphicsPixmapItem>
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!