I have QVector m_vertices and QVector m_lines in my drawingWidget.h
I currently trying to do simple drawing tool, where i can manipulate with vertices and lines. I managed to draw multiple vertices on my "MainWindow", now i am trying to implement line between two vertices
The main idea is, that i have a pop-up menu, where i can choose a "tool". I can add vertex, remove vertex, move vertex, add line, delete line. The idea is , when i choose for example "Add Vertex" then the "m_state" will change to "Adding" so i can only add vertices. When i choose "Add Line" for example, then the "m_state" again will change, so that i can only choose two created vertices, that will result a line between them.
enum DrawingWidgetState {
NO_TOOL_SELECTED,
ADD_VERTEX_SELECTED,
MOVE_VERTEX_SELECTED,
DELETE_VERTEX_SELECTED,
ADD_LINE_SELECTED,
DELETE_LINE_SELECTED
};
Here, in the end, it turns out that if I click "Add Vertex" then when I put the second vertex, then it automatically puts a line between them
void DrawingWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.fillRect(event->rect(), Qt::blue);
painter.setBrush(Qt::black);
for(int i = 0; i < m_vertices.size() ; i++) {
painter.drawEllipse(m_vertices[i], 20, 20);
}
for(int i = 0; i < m_vertices.size()-1 ; i++)
{
const QPoint& point1 = m_vertices[i];
const QPoint& point2 = m_vertices[i+1];
painter.drawLine(point1, point2);
}
}
void DrawingWidget::mousePressEvent(QMouseEvent *event) {
if(m_state == ADD_VERTEX_SELECTED) {
if(event->button() == Qt::LeftButton) {
//m_x = event->x();
//m_y = event->y();
//update();
QPoint point = event->pos();
m_vertices.append(point);
update();
}
}
if(m_state == ADD_LINE_SELECTED) {
if(event->button() == Qt::LeftButton) {
for(int i = 0; i < m_vertices.size()-1 ; i++) {
const auto &point1 = m_vertices[i];
const auto &point2 = m_vertices[i+1];
}
}
}
}
Could someone explain to me why this happens and what should i do to improve my code ?
If something requires explanation, i will tell.
Related
In my view there are 3 lines. 1 Polyline, and 2 straight lines. I have named them as P1 and s1 and s2
I am not able to select s1 line. If I try to select it , P1 gets
selected ( though I have not clicked on P1 )
Selection of p1 is also not sharp. If I clicked somewhere around P1 (
not on P1 ) still P1 gets selected.
void Widget::on_designButoon_clicked()
{
// For straight line S2
QPolygon net0;
net0 << QPoint(50,180);
net0 << QPoint(600,180);
MyPoly* _poly0 = new MyPoly();
_poly0->DrawPolyline(net0,scene);
scene->addItem(static_cast<QGraphicsPathItem*>(_poly0));
// Same logic for Line S1 and P1
}
MyPoly.h
class MyPoly : public QGraphicsPathItem
{
//Q_OBJECT
public:
explicit MyPoly();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void DrawPolyline(QPolygon polygon);
private:
QPolygon polygon_;
};
MyPoly.cpp
MyPoly::MyPoly()
{}
void MyPoly::DrawPolyline(QPolygon polygon)
{
this->polygon_ = polygon;
QPainterPath pPath;
pPath.addPolygon(polygon);
this->setPen(QPen(QColor("blue"), 2));
this->setPath(pPath);
this->setFlag(QGraphicsItem::ItemIsSelectable);
}
void MyPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
auto copied_option = *option;
copied_option.state &= ~QStyle::State_Selected;
auto selected = option->state & QStyle::State_Selected;
QGraphicsPathItem::paint(painter, &copied_option, widget);
if (selected) {
painter->save();
painter->setBrush(Qt::NoBrush);
painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
if(contains(currentCursorPos))
painter->drawPath(shape());
painter->restore();
}
}
bool MyPoly::contains(const QPointF &point) const
{
for(int i=0;i < polygon_.count() - 1; i++)
{
QPointF firstPoint = polygon_.at(i);
QPointF lastPoint = polygon_.at(i+1);
if(firstPoint.x() == lastPoint.x())
{
qDebug()<<"Inside vertical line ";
//It is a vertical line
if(firstPoint.x() == point.x() &&
point.y() >= firstPoint.y() && point.y() <= lastPoint.y())
return true;
}
else
{
// it is a horizontal line
if(point.x() >= firstPoint.x() && point.x() <= lastPoint.x() &&
(firstPoint.y() - 3 <= point.y()) && (point.y() <= lastPoint.y() + 3 ))
return true;
}
}
return false;
}
From looking at the Qt source code it appears that QGraphicsView determines which QGraphicsItem(s) are located at the clicked-on point by calling their contains() method.
Assuming you haven't re-implemented MyPoly::contains(const QPointF &) const to do something more specific, that means that Qt is calling the default QGraphicsItem::contains(const QPointF &) const method, which is implemented like this:
bool QGraphicsItem::contains(const QPointF &point) const
{
return isClipped() ? clipPath().contains(point) : shape().contains(point);
}
... which in turn calls QPainterPath::contains(const QPointF &) const, which uses either the Non-zero winding rule or the Even-Odd rule (depending on the QPainterPath's fillRule setting) to determine if the point is in the path. In either case it looks like the algorithm is designed to detect whether the point is in the interior of a polygon defined by the path and not just "directly on the line", so if you want a more accurate algorithm for your use-case, you'll probably want to override the contains method of your subclass and implement some more precise logic there.
I have a layout that looks like this.
Where:
Blue: rectangle it's a ScrollArea
Orange: rectangles are the widgets from that ScrollArea
My code:
#include <QtWidgets>
///////////////////////////////////////////////////////////////////////////////////////
class RoundedPolygon : public QPolygon {
public:
RoundedPolygon() { SetRadius(10); }
void SetRadius(unsigned int iRadius) { m_iRadius = iRadius; }
const QPainterPath &GetPath() {
m_path = QPainterPath();
if (count() < 3) {
qDebug() << "!! Polygon should have at least 3 points !!";
return m_path;
}
QPointF pt1;
QPointF pt2;
for (int i = 0; i < count(); i++) {
pt1 = GetLineStart(i);
if (i == 0)
m_path.moveTo(pt1);
else
m_path.quadTo(at(i), pt1);
pt2 = GetLineEnd(i);
m_path.lineTo(pt2);
}
// close the last corner
pt1 = GetLineStart(0);
m_path.quadTo(at(0), pt1);
return m_path;
}
private:
QPointF GetLineStart(int i) const {
QPointF pt;
QPoint pt1 = at(i);
QPoint pt2 = at((i + 1) % count());
float fRat = m_iRadius / GetDistance(pt1, pt2);
if (fRat > 0.5f)
fRat = 0.5f;
pt.setX((1.0f - fRat) * pt1.x() + fRat * pt2.x());
pt.setY((1.0f - fRat) * pt1.y() + fRat * pt2.y());
return pt;
}
QPointF GetLineEnd(int i) const {
QPointF pt;
QPoint pt1 = at(i);
QPoint pt2 = at((i + 1) % count());
float fRat = m_iRadius / GetDistance(pt1, pt2);
if (fRat > 0.5f)
fRat = 0.5f;
pt.setX(fRat * pt1.x() + (1.0f - fRat) * pt2.x());
pt.setY(fRat * pt1.y() + (1.0f - fRat) * pt2.y());
return pt;
}
float GetDistance(QPoint pt1, QPoint pt2) const {
int fD = (pt1.x() - pt2.x()) * (pt1.x() - pt2.x()) + (pt1.y() - pt2.y()) * (pt1.y() - pt2.y());
return sqrtf(fD);
}
private:
QPainterPath m_path;
unsigned int m_iRadius{};
};
class PolygonButtonWidget : public QWidget {
Q_OBJECT
public:
explicit PolygonButtonWidget(QWidget *parent = nullptr) : QWidget(parent) {}
~PolygonButtonWidget() override = default;
protected:
void resizeEvent(QResizeEvent *event) override {
float ratioW = 8;
float ratioH = 3;
// ui->scrollAreaWidgetContents->setFixedSize(5000, h);
float thisAspectRatio = (float) event->size().width() / event->size().height();
if (thisAspectRatio < ratioW / ratioH) {
float w = event->size().height() * ratioW / ratioH;
float h = event->size().height();
qDebug() << hasHeightForWidth() << " " << w << " " << h;
this->resize(w, h);
if (m_nrButtons != 0) {
this->move((w + 20) * m_nrButtons, this->y());
}
}
QWidget::resizeEvent(event);
}
int m_nrButtons{};
public:
void setMNrButtons(int mNrButtons) {
m_nrButtons = mNrButtons;
}
protected:
void paintEvent(QPaintEvent *event) override {
int offset = 50;
m_polygon.clear();
m_polygon.emplace_back(0, height()); //DOWN-LEFT
m_polygon.emplace_back(width() - offset, height()); //DOWN-RIGHT
m_polygon.emplace_back(width(), 0); //TOP-RIGHT
m_polygon.emplace_back(0 + offset, 0);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
RoundedPolygon poly;
poly.SetRadius(15);
for (QPoint point: m_polygon) {
poly << point;
}
QBrush fillBrush;
fillBrush.setColor(Qt::darkBlue);
fillBrush.setStyle(Qt::SolidPattern);
QPainterPath path;
path.addPath(poly.GetPath());
painter.fillPath(path, fillBrush);
}
void mousePressEvent(QMouseEvent *event) override {
auto cursorPos = mapFromGlobal(QCursor::pos());
qDebug() << "X: " << cursorPos.x() << " Y: " << cursorPos.y();
inside(cursorPos, m_polygon);
qDebug() << "Pressed";
}
private:
std::vector<QPoint> m_polygon;
bool inside(QPoint point, std::vector<QPoint> polygon) {
auto x = point.x();
auto y = point.y();
auto inside = false;
auto i = 0;
auto j = polygon.size() - 1;
while (i < polygon.size()) {
auto xi = polygon[i].x();
auto yi = polygon[i].y();
auto xj = polygon[j].x();
auto yj = polygon[j].y();
auto intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
j = i++;
}
qDebug() << inside;
return inside;
}
};
///////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget root;
QHBoxLayout layout{&root};
for (int i = 0; i < 10; ++i) {
auto p = new PolygonButtonWidget();
p->setMinimumSize(100, 100);
p->setMNrButtons(i);
layout.addWidget(p);
}
root.setStyleSheet("background-color: rgb(19,19,19);");
QScrollArea view;
view.setWidget(&root);
view.show();
app.exec();
}
#include "main.moc"
The problem arises when I'm trying to resize the window. In the moment of resizing, I want my widgets to keep their aspect ratio. But that's not going to happen.
I have scroll list of widgets which is looking like this (if it's expended on X way too much)
If I will scale it on Y-axis it's going to look like this.
After I've changed the resizeEvent now it's going to look something like this
or like this
How can I fix this? For some reason, some of my widgets are going to disappear, what should be my approach in order to fix this issue?
The problem is caused by the assumption that there's any mechanism that will automatically resize the widgets for you. There isn't. A QScrollArea acts as a layout barrier and any layouts inside of it are isolated from its size, and thus from any resize events.
You must resize the container widget (the one with blue outline on your diagram) yourself anytime the scroll area changes size, and you need first to prepare a test case for the widgets such that their size changes are properly managed when placed in the layout of your choice, and said layout is resized.
Finally, the pet peeve of mine: It's unlikely that you actually need the QMainWindow for anything. It's just a silly Qt Creator template. But unless you want an MDI interface and docking, you shouldn't be using the QMainWindow - and especially not when making a self-contained example. All you need here is QScrollArea as a top-level widget. That's literally all. Any QWidget can be a top-level window!
For future submissions, please provide all the code needed in a single main.cpp file that begins with #include <QtWidgets> and ends with #include "main.moc". You won't need any other includes for Qt classes, and you can write class definitions Java-style, with all the methods defined within the class declaration itself. This provides for short code - after all, a SO question isn't an Enterprise project. It's supposed to be minimal, and that really means that anything not necessary must be removed. No need for header files, multiple includes, nor other fluff - i.e. use Qt containers instead of C++ STL so that you don't need more includes etc.
Your example should look roughly as follows:
#include <QtWidgets>
class PolygonButtonWidget : public QAbstractButton {
Q_OBJECT
/* without seeing the code here, your question is unanswerable */
};
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QWidget root;
QHBoxLayout layout{&root};
PolygonButtonWidget buttons[10];
for (auto &button : buttons)
layout.addWidget(&button);
QScrollArea view;
view.setWidget(&root);
view.show();
app.exec();
view.takeWidget();
}
#include "main.moc"
Without such an example, your question is hard to answer, since:
How can we debug it? Debugging means using a debugger. If your code cannot be immediately compiled, then it's quite unlikely that someone will bother debugging it, and debugging by inspection is often error-prone.
How can we provide a tested answer if we'd have to first write the entire "test case" for it?
How can we know what's inside your button widget? The behavior of that widget does affect the ultimate solution.
It'd also help if you described a few use cases that you'd expect to work. That is, mock up (with a drawing) the state of the widgets before and after the view is resized, so that we can easily see what it is that you expect to happen. A lot of it is very easy to miss when explaining your needs in words. Use cases are a lingua franca of software specifications. If you don't use them, it's highly likely that you yourself don't know what behavior you expect in all cases.
Got a small app where in a QListWidget many QImage and text are displayed with this function:
void TileDisplay::DisplayTiles()
{
for(int i = 0; i < m_tiles.size(); i++) {
QListWidgetItem *item = new QListWidgetItem("Tile " + QString::number(i+1), ui->listWidget);
item->setData(Qt::DecorationRole, m_tiles[i].scaled(64, 64, Qt::IgnoreAspectRatio, Qt::FastTransformation));
}
}
I'm looking for a solution where the QImage is rotated around it's centre with this function:
void TileDisplay::RotateImage(int degree)
{
if(GetTiles().size() > 0) {
QImage *tileToRotate = GetCurrentTile();
if(tileToRotate != nullptr) {
QTransform rotate;
rotate.rotate(degree);
tileToRotate->transformed(rotate);
DisplayTiles();
}
}
}
It is running without any issue, but the image is not rotated.
Any help appreciated.
From the documentation the signature of the QImage::transformed member function is...
QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode) const;
So you need to make use of the returned QImage. Try...
*tileToRotate = tileToRotate->transformed(rotate);
I'm trying to make an horizontal layout with 3 QLabel scale use all its available space. More specifically, this is what I have
this is what I am aiming for
At the moment, the second image is achieved by changing the stylesheet of the qlabels with a slider. Additionally, since I have the the three labels in a layout inside a groupbox, the groupbox resizes to fit its contents, cool.
Now I wanted to drop the slider approach and instead autofit the space available when moving the splitters. In this question, OP reimplements the resizeEvent, and I've seen other posts suggesting the same, changing point by point with this while( !doesFit ) or something similar.
I tried using this approach, both on the resize event and on the splitterMoved event. However, this approach is way prone to feedback loops and other display errors caused. In the other question, they suggest enabling ignoreSizePolicy to prevent the size policy retriggering the sizeevent, but I like how qt handles the size of the layout, how it keeps a minimum size and then it folds the widget if the user insists. Maybe it would work if the HLayout would ignore the resize events triggered by the QLabels, still IMHO unclean thought.
I was wondering if that's the recommended way of achieving this, and wether a less unstable solution exists, maybe using using stylesheets. There are some behaviours that I could also drop, the minimum size limit (so the user could potentially hide the groupbox).
If that's the recommended way of doing it, how should I use the fontmetrics if I have three separate labels, one of which (the number) changes its text dynamically and rapidly? It should not have an impact on performance, and that while loop makes me wary.
It doesn't sound like the while(!fit) approach is going to cut it. Or does it?
--- Edit regarding the duplicate question
Another post creates an event filter, which might also work if reworked to deal with a layout with 3 labels. I finally used a version of the first mentioned post with the variation of the post mentioned in the comments. I'll post the answer if the question is reopened.
One could apply the Newton's method approach from this answer to work on all widgets in a given layout. It will work on any widget with a settable font, not only on a QLabel.
The Newton's algorithm converges reasonably quickly when given a good starting point, e.g. when resizing interactively. It's not atypical to have the loop execute only once. On the other hand, QWidget::sizeHint is integer-valued and and widgets may round fractional font sizes, thus sometimes the iteration is a bit slower than one would expect. The number of iterations is capped to ensure decent performance.
A custom replacement for the label, that provided a QSizeF sizeHintF(), would work better here.
The minimum sizing for the widgets is a bit of a stretch, as the size is not updated as the widget contents change. This could be remedied easily, though.
// https://github.com/KubaO/stackoverflown/tree/master/questions/label-text-size-vert-40861305
#include <QtWidgets>
class LabelStretcher : public QObject {
Q_OBJECT
static constexpr const char kMinimumsAcquired[] = "ls_minimumsAcquired";
static constexpr const char kStretcherManaged[] = "ls_stretcherManaged";
public:
LabelStretcher(QObject *parent = 0) : QObject(parent) {
apply(qobject_cast<QWidget*>(parent));
}
void apply(QWidget *widget) {
if (!widget) return;
setManaged(widget);
setMinimumSize(widget);
widget->installEventFilter(this);
}
void setManaged(QWidget *w, bool managed = true) {
w->setProperty(kStretcherManaged, managed);
}
protected:
bool eventFilter(QObject * obj, QEvent * ev) override {
auto widget = qobject_cast<QWidget*>(obj);
if (widget && ev->type() == QEvent::Resize)
resized(widget);
return false;
}
private:
void onLayout(QLayout *layout, const std::function<void(QWidget*)> &onWidget) {
if (!layout) return;
auto N = layout->count();
for (int i = 0; i < N; ++i) {
auto item = layout->itemAt(i);
onWidget(item->widget());
onLayout(item->layout(), onWidget);
}
}
void setFont(QLayout *layout, const QFont &font) {
onLayout(layout, [&](QWidget *widget){ setFont(widget, font); });
}
void setFont(QWidget *widget, const QFont &font) {
if (!widget || !widget->property(kStretcherManaged).toBool()) return;
widget->setFont(font);
setFont(widget->layout(), font);
}
void setMinimumSize(QWidget *widget) {
if (widget->layout()) return;
widget->setMinimumSize(widget->minimumSizeHint());
}
static int dSize(const QSizeF & inner, const QSizeF & outer) {
auto dy = inner.height() - outer.height();
auto dx = inner.width() - outer.width();
return std::max(dx, dy);
}
qreal f(qreal fontSize, QWidget *widget) {
auto font = widget->font();
font.setPointSizeF(fontSize);
setFont(widget, font);
auto d = dSize(widget->sizeHint(), widget->size());
qDebug() << "f:" << fontSize << "d" << d;
return d;
}
qreal df(qreal fontSize, qreal dStep, QWidget *widget) {
fontSize = std::max(dStep + 1.0, fontSize);
return (f(fontSize + dStep, widget) - f(fontSize - dStep, widget)) / dStep;
}
void resized(QWidget *widget) {
qDebug() << "pre: " << widget->minimumSizeHint() << widget->sizeHint() << widget->size();
if (!widget->property(kMinimumsAcquired).toBool()) {
onLayout(widget->layout(), [=](QWidget *widget){ setMinimumSize(widget); });
widget->setProperty(kMinimumsAcquired, true);
}
// Newton's method
auto font = widget->font();
auto fontSize = font.pointSizeF();
qreal dStep = 1.0;
int i;
for (i = 0; i < 10; ++i) {
auto prevFontSize = fontSize;
auto d = df(fontSize, dStep, widget);
if (d == 0) {
dStep *= 2.0;
continue;
}
fontSize -= f(fontSize, widget)/d;
fontSize = std::max(dStep + 1.0, fontSize);
auto change = fabs(prevFontSize - fontSize)/fontSize;
qDebug() << "d:" << d << " delta" << change;
if (change < 0.01) break; // we're within 1% of target
}
font.setPointSizeF(fontSize);
setFont(widget, font);
qDebug() << "post:" << i << widget->minimumSizeHint() << widget->sizeHint() << widget->size();
}
};
constexpr const char LabelStretcher::kMinimumsAcquired[];
constexpr const char LabelStretcher::kStretcherManaged[];
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QWidget w;
QGridLayout layout{&w};
LabelStretcher stretch{&w};
QLabel labels[6];
QString texts[6] = {"V", "30.0", "kts", "H", "400.0", "ft"};
int i = 0, j = 0, k = 0;
for (auto & label : labels) {
stretch.setManaged(&label);
label.setFrameStyle(QFrame::Box);
label.setText(texts[k++]);
if (j == 0) label.setAlignment(Qt::AlignRight | Qt::AlignVCenter);
else if (j == 1) label.setAlignment(Qt::AlignCenter);
layout.addWidget(&label, i, j++);
if (j >= 3) { i++; j=0; }
}
w.show();
return app.exec();
}
#include "main.moc"
Althought I consider KubaOber's answer better, I'll post this in case it's helpful to someone who wants a solution in the line of the answers mentioned in the post.
Note that the sampletext could be retrieved from the labels as well, the font from the stylesheet, and the code could potentially be placed on a resizeEvent of the groupbox or layout. It wouldn't work on the resizeEvent of the labels since they would compete for the space.
That is one reason why KubaOber answer is superior. Other reasons I can think of is stability given that the 3 labels space differs from the sampletext, thus the font size is not as accurate as it could be. Therefore a resize event could potentially be triggered again by the font change.
static void fitGroupBoxLabels(QGroupBox* groupbox, const QFont &samplefont, const QLayout* const samplelayout)
{
groupbox->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
QString sampletext = "V 1000.0 kts";
QRect availablerect = samplelayout->contentsRect();
if(samplefont.pointSizeF() <= 0) return; //not initalized yet, return
QRect textrect = QFontMetrics(samplefont).boundingRect(sampletext);
if(!textrect.isValid() || !availablerect.isValid()) return; //not initalized yet, return
float factorh = availablerect.width() / (float)textrect.width();
float factorw = availablerect.height() / (float)textrect.height();
float factor = std::min(factorh, factorw);
if (factor < 0.95 || factor > 1.05)
{
float fontSize = samplefont.pointSizeF()*factor;
QString groupBoxStyle = QString("QGroupBox{font-size:8pt} QLabel{font-size:%1pt}").arg(fontSize);
groupbox->setStyleSheet(groupBoxStyle);
}
}
After struggling with this issue, I create DynamicFontSizeLabel and DynamicFontSizePushButton widgets. Hope it helps.
https://github.com/jonaias/DynamicFontSizeWidgets/
I want the height of a QListView based on an QAbstractListModel to fit the contents, if the amount of the items is smaller a given number N. If there are more than N items, it should show only N items. I read a lot of curious tips in the web, but most of them look like hacks. I guess this has something to do with sizeHint() but in the model view approach there is no ItemWidget in which I could override the sizeHint(). What is the correct way to achieve this behaviour?
Further, how does this correlate to the size policy of the parent app? This is a second constraint: The contents should not try to use the space they have in the parent widget, but the parent widget should resize to fit the QListView.
This is not a duplicate to this question, since I can't use QCompleter.
sizeHint() has to be overridden in the QListView (respectively your subclass of it). The mentioned special behaviour can be implemented there. eg like this:
QSize ProposalListView::sizeHint() const
{
if (model()->rowCount() == 0) return QSize(width(), 0);
int nToShow = _nItemsToShow < model()->rowCount() ? _nItemsToShow : model()->rowCount();
return QSize(width(), nToShow*sizeHintForRow(0));
}
This requires the size hint of the item delegate to be reasonable. In my case:
inline QSize sizeHint ( const QStyleOptionViewItem&, const QModelIndex& ) const override { return QSize(200, 48); }
Now I just have to call updateGeometry() after changing the model.
For those who use ListView in QML with QtQuick Controls.
I made the content fit with property anchors.bottomMargin.
anchors.bottomMargin: 20
I have faced the same problem. The solution marked as answer doesn't work correctly when you have scroll bars or you set a frame border. So here is my solution, which is up to date, that works for all QAbstractItemViews.
Since Qt 5.2 version QAbstractItemView has method setSizeAdjustPolicy, inherited from QAbstractScrollArea. If you set QAbstractScrollArea::AdjustToContents than the scroll area will always adjust to the viewport (content).
But the view by default can not be smaller than it's minimum size hint's. So here is what should be done to make an item view fully shrink when the model doesn't have any items:
Set setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents)
Override minimumSizeHint:
QSize minimumSizeHint() const override {
return QSize(0, 0);
}
Override viewportSizeHint (when model has no items, QTreeView and QListView has a default size hint for the viewport, so need to return (0,0) in those cases):
QSize viewportSizeHint() const override {
if (QAbstractItemView::sizeAdjustPolicy() != QAbstractScrollArea::AdjustToContents)
return T::viewportSizeHint();
// if QTableView is used, comment the block below
if (model() == nullptr)
return QSize(0, 0);
if (model()->rowCount() == 0)
return QSize(0, 0);
// T is your view type (QTreeView, QTableView, QListView, etc.)
return T::viewportSizeHint();
}
NOTE:
Don't forget to set sizePolicy or add stretch to the layout.
Not hidden scroll bars increase viewport's minimum size.
Also QListView had problems (bug report) with AdjustToContents flag, that were fixed since Qt 6.2. So if you use a version below 6.2 override viewportSizeHint like this:
QSize viewportSizeHint() const override
{
if (QAbstractItemView::sizeAdjustPolicy() != QAbstractScrollArea::AdjustToContents)
return T::viewportSizeHint();
if (std::is_same<T, QTreeView>::value || std::is_same<T, QListView>::value)
{
if (model() == nullptr)
return QSize(0, 0);
if (model()->rowCount() == 0 || model()->columnCount() == 0)
return QSize(0, 0);
}
if (std::is_same<T, QListView>::value)
{
const int rowCount = model()->rowCount();
int height = 0;
for (int i = 0; i < rowCount; i++) {
height += T::sizeHintForRow(i);
}
return QSize(T::viewportSizeHint().width(), height);
}
return T::viewportSizeHint();
}
There is no good way to do this. I use the following code.
Header:
class List_view_auto_height : public QObject {
Q_OBJECT
public:
explicit List_view_auto_height(QListView * target_list);
void set_max_auto_height(int value);
void set_min_height(int value);
private:
QListView* list;
QTimer timer;
int _min_height;
int _max_height;
bool eventFilter(QObject* object, QEvent* event);
private slots:
void update_height();
};
Source:
List_view_auto_height::List_view_auto_height(QListView *target_list) :
QObject(target_list)
, list(target_list)
{
_min_height = 0;
_max_height = 250;
connect(list->model(), &QAbstractItemModel::rowsInserted,
this, &List_view_auto_height::update_height);
connect(list->model(), &QAbstractItemModel::rowsRemoved,
this, &List_view_auto_height::update_height);
connect(list->model(), &QAbstractItemModel::layoutChanged,
this, &List_view_auto_height::update_height);
list->installEventFilter(this);
update_height();
connect(&timer, &QTimer::timeout, this, &List_view_auto_height::update_height);
timer.start(500);
}
void List_view_auto_height::set_max_auto_height(int value) {
_max_height = value;
update_height();
}
void List_view_auto_height::set_min_height(int value) {
_min_height = value;
update_height();
}
bool List_view_auto_height::eventFilter(QObject *object, QEvent *event) {
if (event->type() == QEvent::Show) {
update_height();
}
return false;
}
void List_view_auto_height::update_height() {
if (!list->isVisible()) { return; }
int height = 0;
if (list->model()->rowCount() > 0) {
height = list->visualRect(list->model()->index(list->model()->rowCount() - 1, 0)).bottom() + 1;
height -= list->visualRect(list->model()->index(0, 0)).top();
}
if (list->horizontalScrollBar()->isVisible()) {
height += list->horizontalScrollBar()->height();
}
bool scrollbar_enabled = false;
if (_max_height != 0 && height > _max_height) {
height = _max_height;
scrollbar_enabled = true;
}
if (height < _min_height) {
height = _min_height;
}
list->setFixedHeight(height + 6);
}
Usage:
new List_widget_auto_height(list);
It's full of hacks and can work incorrectly in some cases. Feel free to improve it.
It sets height using setFixedHeight. This should provide correct behavior for parent widget's size hint.