Last active
September 22, 2018 19:03
-
-
Save jonasbuckner/2acc1a960e457946ce4756199de3fb57 to your computer and use it in GitHub Desktop.
QScrollArea Not Drawing Correctly on Scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWidgets> | |
#include "repeatingwidget.h" | |
#include "mainwindow.h" | |
MainWindow::MainWindow(QWidget *parent) | |
: QMainWindow(parent) | |
{ | |
QMenu *filemenu = menuBar()->addMenu(tr("File")); | |
filemenu->addAction(tr("Quit"), this, &QWidget::close); | |
auto *centralWidget = new QWidget; | |
scrollArea = new QScrollArea; | |
repeatingArea = new RepeatingWidget(); | |
scrollArea->setWidget(repeatingArea); | |
auto *centralLayout = new QVBoxLayout; | |
centralLayout->addWidget(scrollArea, 1); | |
centralWidget->setLayout(centralLayout); | |
setCentralWidget(centralWidget); | |
setWindowTitle(tr("Widget Test")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MAINWINDOW_H | |
#define MAINWINDOW_H | |
#include <QMainWindow> | |
#include "repeatingwidget.h" | |
QT_BEGIN_NAMESPACE | |
class QScrollArea; | |
QT_END_NAMESPACE | |
class RepeatingWidget; | |
class MainWindow : public QMainWindow | |
{ | |
Q_OBJECT | |
public: | |
MainWindow(QWidget *parent = nullptr); | |
~MainWindow(); | |
private: | |
QScrollArea *scrollArea; | |
RepeatingWidget *repeatingArea; | |
}; | |
#endif // MAINWINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWidgets> | |
#include "repeatingwidget.h" | |
RepeatingWidget::RepeatingWidget(QWidget *parent) : QWidget(parent) | |
{ | |
itemHeight = 64; | |
displayFont = QFont("Luxi Serif", 16.0, 1, false); | |
displayItems.append({"first"}); | |
displayItems.append({"second"}); | |
displayItems.append({"third"}); | |
displayItems.append({"fourth"}); | |
displayItems.append({"fifth"}); | |
displayItems.append({"sixth"}); | |
} | |
QSize RepeatingWidget::sizeHint() const { | |
return QSize(500, itemHeight * displayItems.size() + 1); | |
} | |
void RepeatingWidget::paintEvent(QPaintEvent *event) { | |
QPainter painter(this); | |
painter.fillRect(event->rect(), QBrush(Qt::white)); | |
painter.setFont(displayFont); | |
QRect itemRect = event->rect(); | |
int top = itemRect.top(); | |
QFontMetrics fontMetrics(*displayFont); | |
for (auto item : displayItems) { | |
painter.setPen(QPen(Qt::gray)); | |
painter.drawRect(itemRect.left(), top, itemRect.right(), itemHeight); | |
painter.setPen(QPen(Qt::black)); | |
painter.drawText(8, 4 + top + fontMetrics.ascent(), item.name); | |
top += itemHeight; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef REPEATINGWIDGET_H | |
#define REPEATINGWIDGET_H | |
#include <QWidget> | |
#include <QFont> | |
struct RepeatedItem { | |
QString name; | |
}; | |
class RepeatingWidget : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit RepeatingWidget(QWidget *parent = nullptr); | |
QSize sizeHint() const override; | |
protected: | |
void paintEvent(QPaintEvent *event) override; | |
private: | |
QList<RepeatedItem> displayItems; | |
int itemHeight; | |
QFont displayFont; | |
}; | |
#endif // REPEATINGWIDGET_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment