Scroll bars with QScrollArea

Viewed 531

I've created two scroll areas in Qt Designer. Each of them has size [(31,48) 1141 x 161] and [(31, 310) 1141 x 101], respectively. Then I've created two widgets copy of TimeDiagram class, I've set them sizes that are bigger than sizes of the respective scroll areas. And then I've set the objects to the areas. Vertical and Horizontal scroll bars are enabled.

Constructor of the window:

GraphicScaleDialog::GraphicScaleDialog(OutputData *outputData, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GraphicScaleDialog) {
    ui->setupUi(this);

    data = outputData;

    commonSAGridSpacing = 120;
    commonSAScale = 15;
    resultsSAGridSpacing = 120;
    resultsSAScale = 15;

    ui->commonSAGridSpacing_line->setText(QString::number(commonSAGridSpacing));
    ui->commonSAScale_line->setText(QString::number(commonSAScale));
    ui->resultsSAGridSpacing_line->setText(QString::number(resultsSAGridSpacing));
    ui->resultsSAScale_line->setText(QString::number(resultsSAScale));

    commonArea = ui->commonScrollArea;
    commonArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    commonArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    resultsArea = ui->resultsScrollArea;
    resultsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    resultsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    commonDiagramWidget = new TimeDiagram(20, this);
    commonDiagramWidget->setGeometry(31, 41, 2000, static_cast<int>(data->outputSettings.flowQuantity * commonDiagramWidget->rowHeight));
    resultsDiagramWidget = new TimeDiagram(100, this);
    resultsDiagramWidget->setGeometry(31, 114, 2000, 101);

    commonArea->setWidget(commonDiagramWidget);
    resultsArea->setWidget(resultsDiagramWidget);

    commonArea->show();
    resultsArea->show();
}

Diagrams

The problem is to create two grids with timeline axis.

Is it right that I need to use QPainter for this problem?

UPD: May be problem in methods of TimeDiagram?

void TimeDiagram::paintEvent(QPaintEvent * /* event */) {
    QPainter  painter(this);
    draw(&painter);
}

//--------------------------------------------------------------

void TimeDiagram::draw(QPainter *painter) {
    painter->setRenderHint(QPainter ::NonCosmeticDefaultPen, true);
    QPen* pen = new QPen();
    pen->setWidth(1);

    pen->setColor(Qt::black);
    painter->setPen(*pen);
    for (int i = static_cast<int>(rowHeight); i < this->height(); i += static_cast<int>(rowHeight))
    //for (unsigned int counter = 0; counter < static_cast<GraphicScaleDialog *>(this->parent())->data->outputSettings.flowQuantity; counter++)
        painter->drawLine(0, i, this->width(), i);

    pen->setColor(Qt::lightGray);
    painter->setPen(*pen);
    for(int i = 0; i < this->width(); i += 30)
      painter->drawLine(i, 0, i, this->height());

    delete pen;
}
2 Answers

In Designer, in your QScrollArea uncheck widgetResizable property or use QScrollArea::setWidgetResizable to turn it off programatically.

Your TimeDiagram widget should override sizeHint(), so to tell layouts its preferred size. This is used by QScrollArea to determine the displayed widgets's size.

The base QWidget's implementation will use the minimum size, so this should work:

commonDiagramWidget->setMinimumSize(2000, static_cast<int>(data->outputSettings.flowQuantity * commonDiagramWidget->rowHeight));
resultsDiagramWidget->setMinimumSize(2000, 101);

See QScrollArea docs

Related