Custom Widget put in ScrollArea with no scrollbar
-
I'm defining my own Widget with overwriting paintEvent() method. In paintEvent() method, I paint image and hope to display it in QScrollArea, the image displayed and bigger than window, but the scrollbar does not show. I'm curious about the reason, when call qDebug(), my custom widget's size almost always equals to scroll area size, I guess it's the reason. Am I right? How to solve it? Thanks a lot.
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->scrollArea->setWidgetResizable(true); QVBoxLayout* layout = new QVBoxLayout(ui->scrollArea->widget()); ImageWidget* imgWidget = new ImageWidget(this); layout->addWidget(imgWidget); } ImageWidget::ImageWidget(QWidget* parent) : QWidget { parent } { // setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_pixmap.load("image_path"); update(); } void ImageWidget::paintEvent(QPaintEvent* event) { QPainter painter { this }; qDebug("image width and height: %d*%d", m_pixmap.width(), m_pixmap.height()); qDebug() << "current widget width and height: " << this->size(); painter.drawPixmap(0, 0, m_pixmap); } QSize ImageWidget::sizeHint() { return m_pixmap.size(); }
-
Hi and welcome to devnet
Which version of Qt ?
On which platform ?
What if you use a QLabel like shown in the Qt documentation of QScrollArea ?
Did you check the scroll bar policies ? -
Hi and welcome to devnet
Which version of Qt ?
On which platform ?
What if you use a QLabel like shown in the Qt documentation of QScrollArea ?
Did you check the scroll bar policies ?@SGaist
Thank you for watching, my Qt version is Qt6, on Macbook, and it works normally by QLabel. Before your answer, the scroll bar policy is default, after your answer, I tried "AlwaysOn", it did not work. This is my trial.ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
-
One of the issue you likely have is that you only return the size hint, which is "only" a hint. You should also adjust the widget geometry to match your image size.
-
Hi and welcome to devnet
Which version of Qt ?
On which platform ?
What if you use a QLabel like shown in the Qt documentation of QScrollArea ?
Did you check the scroll bar policies ? -
Hi,
-
Create and insert "QScrollArea" object in your "QWidget" -> "m_pcoMyScrollArea"
-
Create your own "QYourLabel" class derived from "QLabel",
the parent is "m_pcoMyScrollArea" -
In the "QYourLabel" class do the following:
m_dScaleFactor = 1.0; // double - 1.0 or other value m_pcoImage = new QImage(); m_pcoImage->load("YourFilePath"); setPixmap(QPixmap::fromImage(*m_pcoImage)); QSize coImageSize(int(pixmap().width() * m_dScaleFactor), int(pixmap().height() * m_dScaleFactor)); resize(coImageSize); // View and set scroll values m_pcoScrollArea->horizontalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->horizontalScrollBar()->value()) + int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->horizontalScrollBar()->pageStep()/2))); m_pcoScrollArea->verticalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->verticalScrollBar()->value()) + int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->verticalScrollBar()->pageStep()/2)));
-
-
One of the issue you likely have is that you only return the size hint, which is "only" a hint. You should also adjust the widget geometry to match your image size.
-
Hi,
-
Create and insert "QScrollArea" object in your "QWidget" -> "m_pcoMyScrollArea"
-
Create your own "QYourLabel" class derived from "QLabel",
the parent is "m_pcoMyScrollArea" -
In the "QYourLabel" class do the following:
m_dScaleFactor = 1.0; // double - 1.0 or other value m_pcoImage = new QImage(); m_pcoImage->load("YourFilePath"); setPixmap(QPixmap::fromImage(*m_pcoImage)); QSize coImageSize(int(pixmap().width() * m_dScaleFactor), int(pixmap().height() * m_dScaleFactor)); resize(coImageSize); // View and set scroll values m_pcoScrollArea->horizontalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->horizontalScrollBar()->value()) + int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->horizontalScrollBar()->pageStep()/2))); m_pcoScrollArea->verticalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->verticalScrollBar()->value()) + int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->verticalScrollBar()->pageStep()/2)));
@Zbigniew-Sch
Thank you for your answer, I have solved my problem. I created custom MyWidget derived from QWidget and overwrited paintEvent method. Then called setGeometry in constructor method and called resize() when scaled. It works as I want. -
-