How to programatically get the margin width of a QTableWidgetItem? - c++

I think the title speaks for itself: Given a QTableWidget with items added by the setItem member function, I want to know what the margin is for each item. In particular, I want the width of the left margin in these cells.

I have prepared a little example computing text margins (a space between item rectangle and text content rectangle) of an item users clicks on:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow mainWin;
QTableWidget* table = new QTableWidget(3, 3, &mainWin);
table->setItem(0, 0, new QTableWidgetItem("Item A"));
table->setItem(1, 0, new QTableWidgetItem("Item B"));
table->setItem(2, 0, new QTableWidgetItem("Item C"));
table->setItem(0, 1, new QTableWidgetItem("Item D"));
table->setItem(1, 1, new QTableWidgetItem("Item E"));
table->setItem(2, 1, new QTableWidgetItem("Item F"));
table->setItem(0, 2, new QTableWidgetItem("Item G"));
table->setItem(1, 2, new QTableWidgetItem("Item H"));
table->setItem(2, 2, new QTableWidgetItem("Item I"));
mainWin.setCentralWidget(table);
mainWin.show();
auto slot = [&table](QTableWidgetItem* item){
QStyleOptionViewItem option;
option.font = item->font();
option.fontMetrics = QFontMetrics(item->font());
if (item->textAlignment())
option.displayAlignment = static_cast<Qt::Alignment>(item->textAlignment());
else
option.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter; // default alignment
option.features |= QStyleOptionViewItem::HasDisplay;
option.text = item->text();
option.rect = table->visualItemRect(item);
// If your table cells contain also decorations or check-state indicators,
// you have to set also:
// option.features |= QStyleOptionViewItem::HasDecoration;
// option.icon = ...
// option.decorationSize = ...
QRect textRect = table->style()->subElementRect(QStyle::SE_ItemViewItemText, &option, nullptr);
double leftMargin = textRect.left() - option.rect.left();
double rightMargin = option.rect.right() - textRect.right();
double topMargin = textRect.top() - option.rect.top();
double bottomMargin = option.rect.bottom() - textRect.bottom();
qDebug() << leftMargin;
qDebug() << rightMargin;
qDebug() << topMargin;
qDebug() << bottomMargin;
};
QObject::connect(table, &QTableWidget::itemClicked, slot);
return app.exec();
}
EDIT
To compute an exact space between table cell border and the text pixels, you have to use a QFontMetrics class.
See the QFontMetrics::leftBearing() and QFontMetrics::tightBoundingRect().

Related

QStackedWidget and subclassed widgets showing when they shouldn't

I'm trying to use a QStackedWidget with a custom QWidget subclass. I'm working on a simple kiosk for my house that shows one of a set of widgets when the screen is tapped. This works very well IFF I create all the stacked widgets in the class that contains the QStackedWidget. When I try to add a subclassed QWidget (lots of labels, need a container), then it displays correctly, but whenever setCurrentIndex() == 0, the subclassed QWidget's showEvent() is called. This means that the first showing has two widgets visible (see image below). Because no hideEvent is ever called for my subclassed widget, it remains visible forever. Below though, you can see that hideEvent is called when the index moves away from this widget, though it immediately gets a showEvent again.
I can hide the subclassed QWidget, but it's complicated if I can't tell that the showEvent I'm getting is the one for me, so skipping it is hard.
I see a showEvent in my subclass whenever QStackedWidget's index == 0.
I have included a short example. This example is a subset but will show the issue, as I stripped out some of the extra functions that don't impact the example (MQTT, setters/getters). I'm running KUbuntu 20.04, Qt5.12.1. I have not found a bug report related to this behavior, but I am hoping I just missed it.
Edited to udpate code examples and debug output
HomeInfo.h
#ifndef HOMEINFO_H
#define HOMEINFO_H
#include <QtCore/QtCore>
#include <QtGui/QtGui>
#include <QtWidgets/QtWidgets>
#include <QtQmqtt/QtQmqtt>
#include "resizelabel.h"
#include "trafficlightwidget.h"
#include "weatherwidget.h"
class HomeInfo : public QMainWindow
{
Q_OBJECT
public:
explicit HomeInfo(QWidget *parent = nullptr);
~HomeInfo() override;
protected:
bool event(QEvent *e) override;
private:
QLabel *m_wallClockLabel;
QLabel *m_dateLabel;
QLabel *m_officeHumidityLabel;
QLabel *m_officeTempLabel;
QLabel *m_atmosphericPressureLabel;
QLabel *m_basementHumidityLabel;
QLabel *m_basementHumidityLabelName;
QLabel *m_basementTemperatureLabel;
QLabel *m_basementTemperatureLabelName;
QWidget *m_trafficLight;
QStackedWidget *m_stack;
QWidget *m_clockGridWidget;
QGridLayout *m_clockWidgetLayout;
QWidget *m_houseGridWidget;
QGridLayout *m_houseGridLayout;
WeatherWidget *m_weatherWidget;
};
#endif // HOMEINFO_H
main application HomeInfo.cpp
#include "homeinfo.h"
HomeInfo::HomeInfo(QWidget *parent) : QMainWindow(parent)
{
QPalette pal(QColor(0,0,0));
setBackgroundRole(QPalette::Window);
pal.setColor(QPalette::Window, Qt::black);
setAutoFillBackground(true);
setPalette(pal);
QFont c("Roboto-Regular", 84);
c.setBold(true);
QFont d("Roboto-Regular", 32);
QFont t("Roboto-Regular", 24);
QFont n("Roboto-Regular", 18);
m_stack = new QStackedWidget(this);
m_clockGridWidget = new QWidget();
m_clockWidgetLayout = new QGridLayout(m_clockGridWidget);
m_wallClockLabel = new QLabel();
m_wallClockLabel->setScaledContents(true);
m_wallClockLabel->setFont(c);
m_wallClockLabel->setAlignment(Qt::AlignCenter);
m_dateLabel = new QLabel();
m_dateLabel->setScaledContents(true);
m_dateLabel->setFont(d);
m_dateLabel->setAlignment(Qt::AlignCenter);
m_officeHumidityLabel = new QLabel();
m_officeHumidityLabel->setScaledContents(true);
m_officeHumidityLabel->setFont(t);
m_officeHumidityLabel->setAlignment(Qt::AlignCenter);
m_officeTempLabel = new QLabel();
m_officeTempLabel->setScaledContents(true);
m_officeTempLabel->setFont(t);
m_officeTempLabel->setAlignment(Qt::AlignCenter);
m_trafficLight = new QWidget();
m_clockWidgetLayout->setHorizontalSpacing(20);
m_clockWidgetLayout->addWidget(m_wallClockLabel, 0, 0, 3, 6);
m_clockWidgetLayout->addWidget(m_officeTempLabel, 3, 0, 1, 3);
m_clockWidgetLayout->addWidget(m_officeHumidityLabel, 3, 3, 1, 3);
m_clockWidgetLayout->addWidget(m_trafficLight, 3, 5, 3, 1);
m_clockWidgetLayout->addWidget(m_dateLabel, 4, 0, 2, 5);
m_basementHumidityLabel = new QLabel();
m_basementHumidityLabel->setScaledContents(true);
m_basementHumidityLabel->setAlignment(Qt::AlignCenter);
m_basementHumidityLabel->setFont(t);
m_basementTemperatureLabel = new QLabel();
m_basementTemperatureLabel->setScaledContents(true);
m_basementTemperatureLabel->setAlignment(Qt::AlignCenter);
m_basementTemperatureLabel->setFont(t);
m_basementTemperatureLabelName = new QLabel("Basement Temp");
m_basementTemperatureLabelName->setFont(n);
m_basementHumidityLabelName = new QLabel("Basement Humidity");
m_basementHumidityLabelName->setFont(n);
m_houseGridWidget = new QWidget();
m_houseGridLayout = new QGridLayout(m_houseGridWidget);
m_houseGridLayout->addWidget(m_basementTemperatureLabelName, 0, 0);
m_houseGridLayout->addWidget(m_basementHumidityLabelName, 0, 1);
m_houseGridLayout->addWidget(m_basementTemperatureLabel, 1, 0);
m_houseGridLayout->addWidget(m_basementHumidityLabel, 1, 1);
connect(m_stack, &QStackedWidget::currentChanged, this, &HomeInfo::stackWidgetChanged);
m_weatherWidget = new WeatherWidget();
m_weatherWidget->setVisible(false);
m_stack->addWidget(m_clockGridWidget);
m_stack->addWidget(m_houseGridWidget);
m_stack->addWidget(m_weatherWidget);
setCentralWidget(m_stack);
}
HomeInfo::~HomeInfo() = default;
void HomeInfo::stackWidgetChanged(int index)
{
qDebug() << __PRETTY_FUNCTION__;
Q_UNUSED(index)
}
bool HomeInfo::event(QEvent *e)
{
if (e->type() == QEvent::MouseButtonRelease) {
qDebug() << __PRETTY_FUNCTION__;
if (m_stack->currentIndex() == (m_stack->count() - 1)) {
m_stack->setCurrentIndex(0);
}
else {
m_stack->setCurrentIndex(m_stack->currentIndex() + 1);
}
return true;
}
return QMainWindow::event(e);
}
WeatherWidget.h
#ifndef WEATHERWIDGET_H
#define WEATHERWIDGET_H
#include <QtCore/QtCore>
#include <QtWidgets/QtWidgets>
class WeatherWidget : public QWidget
{
Q_OBJECT
public:
WeatherWidget(QWidget *parent = nullptr);
~WeatherWidget() override;
protected:
void hideEvent(QHideEvent *event) override;
void showEvent(QShowEvent *event) override;
private:
QLabel *m_outdoorTemperatureLabel;
QLabel *m_outdoorTemperatureTextLabel;
QLabel *m_outdoorHumidityLabel;
QLabel *m_outdoorHumidityTextLabel;
QLabel *m_luxLabel;
QLabel *m_luxTextLabel;
QLabel *m_uvRawLabel;
QLabel *m_uvRawTextLabel;
QLabel *m_uvIndexLabel;
QLabel *m_uvIndexTextLabel;
QLabel *m_rainTodayLabel;
QLabel *m_rainTodayTextLabel;
QLabel *m_totalRainLabel;
QLabel *m_totalRainTextLabel;
QLabel *m_airPressureMercuryLabel;
QLabel *m_airPressureMercuryTextLabel;
QLabel *m_airPressureTrendLabel;
QLabel *m_airPressureTrendTextLabel;
QGridLayout *m_layout;
};
#endif // WEATHERWIDGET_H
WeatherWidget.cpp
#include "weatherwidget.h"
WeatherWidget::WeatherWidget(QWidget *parent) : QWidget(parent)
{
QDate now = QDate::currentDate();
QFont text("Roboto-Sans", 14);
QFont content("Roboto-Sans", 20);
m_outdoorTemperatureLabel = new QLabel();
m_outdoorTemperatureLabel->setFont(content);
m_outdoorTemperatureLabel->setAlignment(Qt::AlignCenter);
m_outdoorHumidityLabel = new QLabel();
m_outdoorHumidityLabel->setFont(content);
m_outdoorHumidityLabel->setAlignment(Qt::AlignCenter);
m_luxLabel = new QLabel();
m_luxLabel->setFont(content);
m_luxLabel->setAlignment(Qt::AlignCenter);
m_uvIndexLabel = new QLabel();
m_uvIndexLabel->setFont(content);
m_uvIndexLabel->setAlignment(Qt::AlignCenter);
m_rainTodayLabel = new QLabel();
m_rainTodayLabel->setFont(content);
m_rainTodayLabel->setAlignment(Qt::AlignCenter);
m_totalRainLabel = new QLabel();
m_totalRainLabel->setFont(content);
m_totalRainLabel->setAlignment(Qt::AlignCenter);
m_airPressureMercuryLabel = new QLabel();
m_airPressureMercuryLabel->setFont(content);
m_airPressureMercuryLabel->setAlignment(Qt::AlignCenter);
m_airPressureTrendLabel = new QLabel();
m_airPressureTrendLabel->setFont(content);
m_airPressureTrendLabel->setAlignment(Qt::AlignCenter);
m_outdoorTemperatureTextLabel = new QLabel("Temperature");
m_outdoorTemperatureTextLabel->setFont(text);
m_outdoorTemperatureTextLabel->setAlignment(Qt::AlignCenter);
m_outdoorHumidityTextLabel = new QLabel("Humidity");
m_outdoorHumidityTextLabel->setFont(text);
m_outdoorHumidityTextLabel->setAlignment(Qt::AlignCenter);
m_luxTextLabel = new QLabel("Brightness");
m_luxTextLabel->setFont(text);
m_luxTextLabel->setAlignment(Qt::AlignCenter);
m_uvIndexTextLabel = new QLabel("UV Index");
m_uvIndexTextLabel->setFont(text);
m_uvIndexTextLabel->setAlignment(Qt::AlignCenter);
m_rainTodayTextLabel = new QLabel("Rainfall Today");
m_rainTodayTextLabel->setFont(text);
m_rainTodayTextLabel->setAlignment(Qt::AlignCenter);
m_totalRainTextLabel = new QLabel();
m_totalRainTextLabel->setFont(text);
m_totalRainTextLabel->setAlignment(Qt::AlignCenter);
m_airPressureMercuryTextLabel = new QLabel("Air Pressure");
m_airPressureMercuryTextLabel->setFont(text);
m_airPressureMercuryTextLabel->setAlignment(Qt::AlignCenter);
m_airPressureTrendTextLabel = new QLabel("Air Pressure Trend");
m_airPressureTrendTextLabel->setFont(text);
m_airPressureTrendTextLabel->setAlignment(Qt::AlignCenter);
m_layout = new QGridLayout(this);
m_layout->addWidget(m_outdoorTemperatureTextLabel, 0, 0);
m_layout->addWidget(m_outdoorTemperatureLabel, 1, 0);
m_layout->addWidget(m_outdoorHumidityTextLabel, 0, 1);
m_layout->addWidget(m_outdoorHumidityLabel, 1, 1);
m_layout->addWidget(m_airPressureMercuryTextLabel, 0, 2);
m_layout->addWidget(m_airPressureMercuryLabel, 1, 2);
m_layout->addWidget(m_luxTextLabel, 2, 0);
m_layout->addWidget(m_luxLabel, 3, 0);
m_layout->addWidget(m_uvIndexTextLabel, 2, 1);
m_layout->addWidget(m_uvIndexLabel, 3, 1);
m_layout->addWidget(m_airPressureTrendTextLabel, 2, 2);
m_layout->addWidget(m_airPressureTrendLabel, 3, 2);
m_layout->addWidget(m_rainTodayTextLabel, 4, 0);
m_layout->addWidget(m_rainTodayLabel, 5, 0);
m_layout->addWidget(m_totalRainTextLabel, 4, 1);
m_layout->addWidget(m_rainTodayLabel, 5, 1);
setLayout(m_layout);
m_totalRainTextLabel->setText(QString("%1 Rainfall").arg(now.year()));
}
WeatherWidget::~WeatherWidget()
{
}
void WeatherWidget::showEvent(QShowEvent* event)
{
qDebug() << __PRETTY_FUNCTION__;
}
void WeatherWidget::hideEvent(QHideEvent* event)
{
qDebug() << __PRETTY_FUNCTION__;
}
main.cpp
#include "homeinfo.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HomeInfo w;
app.setOverrideCursor(QCursor(Qt::BlankCursor));
w.setGeometry(0, 0, 800, 480);
w.show();
return app.exec();
}
Debug output
void HomeInfo::stackWidgetChanged(int) : Index 0 // constructor
virtual void WeatherWidget::showEvent(QShowEvent*) // constructor
virtual bool HomeInfo::event(QEvent*) // touchevent
void HomeInfo::stackWidgetChanged(int) : Index 1
virtual bool HomeInfo::event(QEvent*) //touchevent
void HomeInfo::stackWidgetChanged(int) : Index 2
virtual bool HomeInfo::event(QEvent*) // touchevent
virtual void WeatherWidget::hideEvent(QHideEvent*) //WeatherWidget gets hidden
void HomeInfo::stackWidgetChanged(int) : Index 0 // signal that index changed to HomeInfo
virtual void WeatherWidget::showEvent(QShowEvent*) // WeatherWidget gets another showEvent
virtual void WeatherWidget::hideEvent(QHideEvent*) // after X closes window
virtual void WeatherWidget::hideEvent(QHideEvent*) // after X closes window
*** Exited normally ***```
No, this was just a dumb mistake. I used autocomplete when I attempted to type setLux() in HomeInfo.cpp (not visible, I removed the MQTT functions), and what I got was setVisible(). This meant that every time I got a new value for how bright it is outside, it was setVisible(true) on the widget when the double was implicitly cast to true. I would have expected a warning from gcc, but either warnings are turned off, or it wasn't generated. Ah well, this was not a QT bug or odd unexpected behavior, it was a mistake in my code.

How to rescale GraphicItems without disturbing the shape?

How to rescale GraphicItems without disturbing the shape?
I am trying to rescale QGraphicsItems from a part of a scene. When I rescale the QGraphicsItem it transforms the shape.
QList<QGraphicsItem*> items;
for (auto it : items)
{
qreal scale = 1.75;
QPointF c = it->mapToScene(it->boundingRect().center());
it->setScale(scale);
QPointF cNew = it->mapToScene((it->boundingRect()).center());
QPointF offset = c - cNew;
it->moveBy(offset.x(), offset.y());
}
Create QGraphicsItemGroup which is containing all your items and apply the transform on it.
You just have to recenter the group according to the previous position.
QList<QGraphicsItem*> makeItems()
{
return QList<QGraphicsItem*>() << new QGraphicsLineItem(0, 0, 50, 0)
<< new QGraphicsLineItem(50, 0, 50, 50)
<< new QGraphicsLineItem(50, 50, 0, 50)
<< new QGraphicsLineItem(0, 50, 0, 0)
<< new QGraphicsRectItem(24, 24, 2, 2)
<< new QGraphicsEllipseItem(0, 0, 50, 50);
}
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene(-500, -500, 1000, 1000);
auto* view = new QGraphicsView;
view->setScene(scene);
QList<QGraphicsItem*> items, originalItems;
originalItems = makeItems();
items = makeItems();
QGraphicsItemGroup* itemGroup = new QGraphicsItemGroup;
for (auto* item: originalItems)
{
scene->addItem(item);
}
for (auto* item: items)
{
itemGroup->addToGroup(item);
}
scene->addItem(itemGroup);
qreal scale = 1.75;
QPointF const beforeScale = itemGroup->mapToScene(itemGroup->boundingRect().center());
itemGroup->setScale(scale);
QPointF const afterScale = itemGroup->mapToScene(itemGroup->boundingRect().center());
QPointF const offset = beforeScale - afterScale;
itemGroup->moveBy(offset.x(), offset.y()); // The center will be the same than before the scaling
view->show();
return app.exec();
}
Use QPen::setCosmetic to keep the look-and-feel before and after the transformation

use qt table with buttons

I have a table with some data. However since not all the information fits into the table, the user should have the option to get further information on the line by pressing a button in this line. I currently add the buttons in the following way:
int lastRow = table->rowCount();
table->insertRow(lastRow);
QWidget* pWidget = new QWidget();
pWidget->setFixedWidth(30);
LdtButton* btn_help = new LdtButton();
btn_help->addInactiveIcon(QPixmap(":/icons/help_inactive.png"));
btn_help->addHoverIcon(QPixmap(":/icons/help_hovered.png"));
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(btn_help);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);
table->setCellWidget(lastRow, 1, pWidget);
However I do not really have a idea how to connect these buttons, so I get the row the button was in when it gets pressed, so I can output the appropriate informations. (not every row has a button)
Use the signal QPushButton::clicked and a lambda to call the right method (use the capture to pass the row).
QTableWidget* table = new QTableWidget(0, 2);
QStringList values = {"foo", "bar", "spam"};
for (QString const& value : values)
{
int lastRow = table->rowCount();
table->insertRow(lastRow);
table->setItem(lastRow, 0, new QTableWidgetItem(value));
QWidget* pWidget = new QWidget();
QPushButton* btn_help = new QPushButton("help");
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(btn_help);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);
table->setCellWidget(lastRow, 1, pWidget);
// Call your method in the lambda
QObject::connect(btn_help, &QPushButton::clicked, [lastRow]() {qDebug() << "Show help for " << lastRow; });
}
table->show();
It will display:
Show help for 0
Show help for 1
Show help for 2

rowspan of QGridLayout not working as expected

Here is how to draw a button that spans 2 columns:
#include <QtGui>
int main(int argv, char **args)
{
QApplication app(argv, args);
QPushButton *foo = new QPushButton("foo");
QPushButton *bar = new QPushButton("bar");
QPushButton *baz = new QPushButton("baz");
QGridLayout *layout = new QGridLayout();
layout->addWidget(foo, 0, 0);
layout->addWidget(bar, 0, 1);
layout->addWidget(baz, 1, 0, 1, 2); // span 2 columns
QWidget window;
window.setLayout(layout);
window.setWindowTitle("test");
window.show();
return app.exec();
}
Running the code gives me:
If I change the layout in order to get a button, baz, that spans 2 rows I fail:
layout->addWidget(foo, 0, 0);
layout->addWidget(bar, 1, 0);
layout->addWidget(baz, 0, 1, 2, 1); // (try to) span 2 rows
Here is what I get:
Your layout is fine, the baz button is spanning two rows. The problem is that it doesn't use all the available space. You have to change the vertical resize policy of your button from Fixed to MinimumExpanding.
I added the following, after which all was well:
foo->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
bar->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
baz->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
(thanks)

Displaying an image with QGraphics Scene

Having an issue with displaying a loaded image in a QGraphicsScene.
CTextBox::CTextBox(QWidget* parent /* = NULL */)
{
QPixmap image;
image.load("basketball.png");
grid = new QGridLayout();
grid->setSpacing(1);
textBrowser = new QTextEdit(this);
treeView = new QTreeView;
treeLabel = new QLabel;
treeLabel->setText("Tree View:");
debugLabel = new QLabel;
debugLabel->setText("Debug:");
standardModel = new QStandardItemModel;
rootNode = standardModel->invisibleRootItem();
treeView->setModel(standardModel);
QGraphicsPixmapItem pixmapitem(image);
//scene.addText("Graphics");
scene.addItem(&pixmapitem);
//rect = scene.addRect(QRectF(0,0, 100, 100));
//QGraphicsItem *item = scene.itemAt(50, 50);
widget = new QWidget();
view = new QGraphicsView;
view->setScene(&scene);
view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
//proxy = scene.addWidget(widget);
//label = new QLabel();
grid->addWidget(textBrowser, 1, 0);
grid->addWidget(debugLabel, 0, 0);
grid->addWidget(treeView, 1, 1);
grid->addWidget(treeLabel, 0, 1);
//grid->addWidget(&image, 2,0);
//grid->addWidget(view, 2, 0);
//grid->addWidget(widget, 2, 0);
//grid->addWidget(proxy, 2,0);
view->show();
//widget->setLayout(view);
//widget->show();
setLayout(grid);
//label->addItem()
}
Basically when I try and load the image in a QGraphicsScene it seems to scale up to the size of the image but displays a white screen. I can add text to the graphics scene fine and that will display correctly. Now if I try to do add the image into a label on a different widget it displays fine. Any ideas why?
Cheers.
You don't see it because you pass a reference to a local object of the constructor. The pixmap item will be destructed immediately after your constructor ends. Use dynamic allocation instead.
QGraphicsPixmapItem* pixmapitem = new QGraphicsPixmapItem(image);
scene.addItem(pixmapitem);