QWidget Overflow - c++

I have a main QGridLayout on the dialog I'm working on. It's split into content-containers (QWidgets with their own QVBoxLayout) of varying sizes, each assigned a particular number of grid-columns within the grid layout (depending on their needs).
Each content-container holds some buttons. Some of these buttons should span across 2 content-containers. The way I'm trying to achieve this is by overflowing these 2-container buttons outside their layout, while keeping the size of the content-containers the same (what it was allocated according to the number of columns within the grid). I have no idea how to go about this, because either the size of the containers changes, or the QWidget doesn't overflow.
Is there a way to set some sort of overflow property on the button, or do I have to just place the buttons on the grid? I'm trying to avoid doing this because I think it could be messy once new requirements arise and I'll have to recalculate their positioning.
Here is an image of what I'm trying to do:
Here is the relevant code:
ModeDialog::ModeDialog(MainWindow *pMainWindow)
: XDialog(pMainWindow, tr("Operating Mode"), true)
{
XDialog::AddButton(tr("Exit"), QDialogButtonBox::AcceptRole);
ConstructStylingFromTemplates();
CSettings* Settings = pMainWindow->GetSettings();
QString SlotString1, SlotString2;
QGridLayout* mp_MainLayout = new QGridLayout();
mp_MainLayout->setContentsMargins(10, 30, 10, 20);
// Construct Channel Group Layouts with Channel Containers
for (int i = 0; i < 3; i++)
{
switch(i)
{
case 0: { SlotString1 = "A"; SlotString2 = "B"; break; }
case 1: { SlotString1 = "C"; SlotString2 = "D"; break; }
case 2: { SlotString1 = "E"; SlotString2 = "F"; break; }
}
QHBoxLayout* ChannelGroupLayout = new QHBoxLayout();
if (CSettings_RR::IsE1T1Channel(Settings, i*2))
{
AddChannelToChannelGroup(ChannelGroupLayout, SlotString1);
AddChannelToChannelGroup(ChannelGroupLayout, SlotString2);
}
else if(CSettings_RR::IsPtpChannel(Settings, i*2))
{
AddChannelToChannelGroup(ChannelGroupLayout, SlotString1);
}
else if(CSettings_RR::IsOtaChannel(Settings, i*2))
{
AddChannelToChannelGroup(ChannelGroupLayout, SlotString1);
}
else
{
continue;
}
mp_MainLayout->addLayout(ChannelGroupLayout, 0, i*2, 1, 2);
}
SetContentLayout(mp_MainLayout);}
void ModeDialog::AddChannelToChannelGroup(QHBoxLayout* ChannelGroupLayout, QString SlotString)
{
QVBoxLayout* ChannelLayout = new QVBoxLayout();
// Add Label to Channel Layout
XLabel* ChannelLabel = new XLabel("Channel " + SlotString, m_textSize, true, Qt::AlignCenter | Qt::AlignVCenter, this);
ChannelLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
ChannelLayout->addWidget(ChannelLabel);
// Add Container to Channel Layout
QWidget* ChannelContainer = new QWidget();
ChannelContainer->setStyleSheet(m_styleSheetChannelContainer);
ChannelLayout->addWidget(ChannelContainer);
//WIP - add buttons to container
QVBoxLayout* ChannelContainerLayout = new QVBoxLayout();
ChannelContainer->setLayout(ChannelContainerLayout);
XPushButton* ModeButton = new XPushButton(tr("CLOCK"), 160, 40, this, true);
ChannelContainerLayout->addWidget(ModeButton);
//WIPEND
// Add Channel Layout to Channel Group Layout
ChannelGroupLayout->addLayout(ChannelLayout);
}

It is not possible to overflow using QWidgets.
But it is possible with QML.
By default, items will overflow and you have to manage their positions and sizes (and z-index to tell who overlaps who) : http://doc.qt.io/qt-5/qtquick-positioning-topic.html
You have to use QML layouts or set the item's parent property clip to true to avoid the overflow.

There is no way to overflow widgets in Qt, not even if you don't use a layout. A widget will always be limited to its parent's bounds.

Related

Move layout to another layout in Qt5

I have a custom container widget in Qt5 which has a QFrame without a layout. I use setLayout on QFrame which works fine but sometimes I want to transfer an existing layout which contains widgets and other layouts to that QFrame as layout. Is that even possible in Qt5 to transfer layouts already added with addLayout?
No problem. Just make the layout the layout of the target widget with setLayout(). The widgets contained will be reparented automatically.
void MainWindow::moveLayout()
{
// Move the layout from ui->g0 to ui->g1 and vice versa
// every time you call moveLayout
static int toggle = 0;
QLayout* l;
QWidget* w;
if (toggle == 0)
{
l = ui->g0->layout();
w = ui->g1;
}
else
{
l = ui->g1->layout();
w = ui->g0;
}
if (w->layout())
{
// Hack to clean target widget
QWidget z;
z.setLayout(w->layout());
}
w->setLayout(l);
toggle = 1 - toggle;
}

QListWidget items consistent positioning problem

I'm new to Qt framework. In new application I want to create list with customised items. These items are quite simple and must contain title label, thumbnail and description label (picture here)
For now I don't want to play with custom drawing and all that stuff becuase I think it's easier to do items I want with proper widget/layout so I decided to use QListwidget and subclassed QAbstractItemModel (KeyframeModel) and QListWidgetItem (TileWidgetItem).
After some coding it looks how I wanted but strange thing happens to QListWidget (grid mode) when I add some items. In my case a QListWidget is resizable (due to how it's embedded inside main layout) and number of columns should be dependent on list and items width. Items are fixed size (at least for now).
But when I resize the list one of the items at some list's width is misaligned and not I don't know what's going on. Below are te screenshots from app:
Pic. 1 List initial state (right after start)
Pic. 2 List after resizing #1
Pic. 3 List after resizing #2
Resizing #2 is a few pixels wider than resizing #1 and resizing #1 is hard to get (border case) - few pixels width less and I've got 2 columns (it's okay) but some pixels more and I end up with case #2.
In all cases number of columns is okay.
Sometimes also last item is misaligned after program start right-away like here (right after start like in pic. 1 but as you can see different result despite same list width).
I wonder why is it so inconsistent after start-up.
Do I missing something? Do I must do some parts in different way?
Or is it just some glitches in debug mode?
Below I post some code:
Application:
// Source file
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//--------------------------------------------------------------------------------
// Add elements to the list
TileWidgetItem *item = new TileWidgetItem();
item->setData(TileWidgetItem::TileRole::TitleRole, QVariant("Long title"));
item->setData(TileWidgetItem::TileRole::DescriptionRole, QVariant("My long info"));
item->setText("My super text");
qDebug() << "Widget size hint: " << item->sizeHint();
ui.listWidget_moves->addItem(item);
item->updateView();
TileWidgetItem *item1 = new TileWidgetItem();
item1->setData(TileWidgetItem::TileRole::TitleRole, QVariant("Item #2"));
item1->setText("Tile #2");
ui.listWidget_moves->addItem(item1);
item1->updateView();
TileWidgetItem *item2 = new TileWidgetItem();
ui.listWidget_moves->addItem(item2);
item2->updateView();
TileWidgetItem *item3 = new TileWidgetItem();
ui.listWidget_moves->addItem(item3);
item3->updateView();
//--------------------------------------------------------------------------------
// Adjust cell size
QSize cellSize;
for (uint i = 0; i < ui.listWidget_moves->count(); i++)
{
int dim = ui.listWidget_moves->item(i)->sizeHint().height();
if (dim > cellSize.height())
cellSize.setHeight(dim);
dim = ui.listWidget_moves->item(i)->sizeHint().width();
if (dim > cellSize.width())
cellSize.setWidth(dim);
}
ui.listWidget_moves->setGridSize(cellSize);
}
Item widget:
// Source file
constexpr int MAX_THUMB_SIZE = 100;
TileWidgetItem::TileWidgetItem(QListWidget *listview)
: QListWidgetItem(listview, ItemType::UserType)
{
/* Prepare main widget */
QWidget *view = new QWidget();
view->setObjectName("tile");
view->setStyleSheet(
"QWidget#tile { margin: 4 8; background-color: #404040; border: 1 solid rgba(0,0,0,30%); border-radius: 4px }\n"
"QWidget#tile::hover { border: 1 solid #EEE; background-color: #484848 }\n"
"QWidget#tile[selected=true] { background-color: #00F }"
);
//-----------------------------------------------------------
/* Prepare layout */
QVBoxLayout *layout = new QVBoxLayout();
layout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
//-----------------------------------------------------------
/* Prepare title with icon */
QHBoxLayout *titleLayout = new QHBoxLayout();
QLabel *titleIcon = new QLabel();
titleIcon->setObjectName("titleIcon");
titleIcon->setStyleSheet("background-color: black");
titleIcon->setFixedSize(QSize(16, 16));
titleLayout->addWidget(titleIcon);
QLabel *title = new QLabel("Title");
title->setObjectName("title");
title->setMinimumWidth(60);
title->setStyleSheet("background-color: #800;");
titleLayout->addWidget(title);
QWidget *titleWidget = new QWidget();
titleWidget->setStyleSheet("background-color: #080");
titleWidget->setLayout(titleLayout);
layout->addWidget(titleWidget);
//-----------------------------------------------------------
/* Prepare thumbnail */
QLabel *thumbnail = new QLabel();
thumbnail->setObjectName("thumbnail");
thumbnail->setStyleSheet("background-color: black; border: 1 solid #F00");
thumbnail->setFixedSize(QSize(MAX_THUMB_SIZE, MAX_THUMB_SIZE * 0.7f));
thumbnail->setPixmap(QPixmap("Resources/moto.jpg").scaledToWidth(MAX_THUMB_SIZE));
layout->addWidget(thumbnail);
//-----------------------------------------------------------
/* Preparing additional info */
QLabel *description = new QLabel("Description");
description->setObjectName("description");
//description->setToolTip("Custom info tip");
description->setContentsMargins(4, 2, 4, 2);
layout->addWidget(description);
//-----------------------------------------------------------
view->setLayout(layout);
_customView = view;
_titleView = title;
_descriptionView = description;
setSizeHint(_customView->sizeHint());
updateView();
}
TileWidgetItem::~TileWidgetItem()
{
}
void TileWidgetItem::setData(int role, const QVariant &value)
{
QListWidgetItem::setData(role, value);
if (value.type() == QVariant::Type::String)
{
if (role == TileRole::TitleRole)
{
this->_titleView->setText(value.toString());
}
else if (role == TileRole::DescriptionRole)
{
this->_descriptionView->setText(value.toString());
}
setSizeHint(_customView->sizeHint());
}
}
void TileWidgetItem::updateView()
{
if (listWidget() != nullptr)
{
listWidget()->setItemWidget(this, this->_customView);
}
}
// Header file
class TileWidgetItem : public QListWidgetItem
{
public:
enum TileRole
{
TitleRole = Qt::UserRole + 1,
DescriptionRole,
ThumbnailRole
};
public:
TileWidgetItem(QListWidget *listview = nullptr);
~TileWidgetItem();
void setData(int role, const QVariant &value) override;
void updateView();
QWidget *customView() const { return _customView; };
QString getTitle() const { return _titleView->text(); };
QString getInfo() const { return _descriptionView->text(); };
private:
QWidget *_customView;
QLabel *_titleView;
QLabel *_descriptionView;
};
Platform: Windows 10
Qt version: 5.14.2
IDE: Visual Studio 2019 (with Qt VS Tools)
In the end I just used custom delegates which solved problems.
I wanted to overuse system and in I was defeated :)

Widgets with FixedSize change their size at will

I have a QGridLayout inside a center widget, which contains a supposedly fixed-sized widget (referred to as inner widget) that also has a QGridLayout filled with buttons. Inner widget's size is determined by how many buttons are there in the grid, and is supposed to be an exact fit (no spacing between the buttons, FixedSize policy applied in buttons' constructor), and all buttons have their sizes and policies set in the constructor. Now, if I don't put inner widget into a layout of any kind, it works just fine, I get nice square buttons. But if I put inner widget into a grid layout, all buttons suddenly change their sizes, and widget also doesn't seem like keeping its size. Why?
Edit: MyButtonTable:
MyButtonTable::MyButtonTable(QWidget *parent) : QWidget(parent), array()
{
size_x = 2;
size_y = 2;
QGridLayout* layout = new QGridLayout();
for(size_t x = 0; x < size_x; x++) {
this->array.push_back(std::vector<MyRightClickButton*>());
}
for(size_t x = 0; x < size_x; x++) {
for(size_t y = 0; y < size_y; y++) {
this->array[x].push_back(new button_t());
QObject::connect(array[x][y], SIGNAL(rightClicked()), this, SLOT(internalRightClick()));
QObject::connect(array[x][y], SIGNAL(clicked()), this, SLOT(internalClick()));
layout->addWidget(array[x][y], x, y);
}
}
layout->setSpacing(0);
layout->setContentsMargins(0,0,0,0);
this->setLayout(layout);
this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
this->setMinimumSize(QSize(0,0));
this->resize(QSize(10*size_y,10*size_x));
}
MyRightClickButton(QWidget *parent = 0):QPushButton(parent) {
marked = false;
this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
this->setMinimumSize(QSize(0,0));
this->resize(QSize(10,10));
}
A layout manager is used to arrange child widgets in them. The arrangement designates each layout manager. Layout managers adjust the size of their child widgets based on the resize policies. Even if you are setting a fixed size for a child widget, using resize() or setGeometry(), they will be resized by the layout manager, if you did not set the resize policy of the child widget.
For example
widget->setFixedSize (100, 100);
widget->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
This is how it should be done.

Add widgets to QFileDialog

I need to add a widget (QTableWidget) into QFileDialog's layout. I know that it is QGridLayout with sizes (3,4). The table must be in 3-rd row and span all columns.
QTableWidget* tableWidget = new QTableWidget(this);
QGridLayout *layout = static_cast<QGridLayout*>(QFileDialog::layout());
layout->addWidget(tableWidget, 2, 0, 1, 4);
With this code the original 3-rd row which contains lineEdit and save/open pushButton disappears. How can I add widgets between already existing widgets of QGridLayout so that original widgets remain in the layout.
I strongly recommend you not to rely on QFileDialog's implementation. The layout can be different on different platforms or different versions of Qt. It may be more correct to place your table under the dialog or to the right of it. This can be done easily without altering the layout of the QFileDialog itself. Just create a QVBoxLayout and put QFileDialog and QTableWidget inside it.
However, the question has been asked, and the solution exists. QGridLayout has no functionality such as QBoxLayout::insertItem. So we need to implement this behavior manually. The plan is:
Obtain the list of layout items placed in 3rd and 4th rows.
Calculate new positions of items.
Take elements out of item and add them back at new positions.
Working code:
QFileDialog* f = new QFileDialog();
f->setOption(QFileDialog::DontUseNativeDialog, true); //we need qt layout
QGridLayout *layout = static_cast<QGridLayout*>(f->layout());
QList< QPair<QLayoutItem*, QList<int> > > moved_items;
f->show();
for(int i = 0; i < layout->count(); i++) {
int row, column, rowSpan, columnSpan;
layout->getItemPosition(i, &row, &column, &rowSpan, &columnSpan);
if (row >= 2) {
QList<int> list;
list << (row + 1) << column << rowSpan << columnSpan;
moved_items << qMakePair(layout->takeAt(i), list);
i--; // takeAt has shifted the rest items
}
}
for(int i = 0; i < moved_items.count(); i++) {
layout->addItem(moved_items[i].first,
moved_items[i].second[0],
moved_items[i].second[1],
moved_items[i].second[2],
moved_items[i].second[3]);
}
QTableWidget* tableWidget = new QTableWidget();
layout->addWidget(tableWidget, 2, 0, 1, 4);

Minimum size/width of a QPushButton that is created from code

I created 2 rows of push buttons, each row is inside a QHBoxLayout.
I create the buttons in the code:
static const char* buttonText = "23456789TJQKA";
for (int ii = 0; buttonText[ii]; ii++)
{
QPushButton* pushButton = new QPushButton(this);
pushButton->setText(QString(buttonText[ii]));
ui->horizontalLayout_1->addWidget(pushButton);
}
for (int ii = 0; buttonText[ii]; ii++)
{
QPushButton* pushButton = new QPushButton(this);
pushButton->setText(QString(buttonText[ii]));
ui->horizontalLayout_2->addWidget(pushButton);
}
The problem is that they can't shrink (when the user resizes the dialog) beyond that size, even though their text would fit in a much smaller width. If I create the buttons manually in the resource editor instead of in the code, they can have smaller width than that.
This happens because the minimumSizeHint of the QPushButton does not allow the QLayout to resize it :
The default implementation of minimumSizeHint() returns an invalid
size if there is no layout for this widget, and returns the layout's
minimum size otherwise. Most built-in widgets reimplement
minimumSizeHint().
QLayout will never resize a widget to a size smaller than the minimum
size hint unless minimumSize() is set or the size policy is set to
QSizePolicy::Ignore. If minimumSize() is set, the minimum size hint
will be ignored.
The simple solution is to set the minimum width explicitly:
static const char* buttonText = "23456789TJQKA";
for (int ii = 0; buttonText[ii]; ii++)
{
QPushButton* pushButton = new QPushButton(this);
pushButton->setMinimumWidth(5);
pushButton->setText(QString(buttonText[ii]));
ui->horizontalLayout_1->addWidget(pushButton);
}
for (int ii = 0; buttonText[ii]; ii++)
{
QPushButton* pushButton = new QPushButton(this);
pushButton->setMinimumWidth(5);
pushButton->setText(QString(buttonText[ii]));
ui->horizontalLayout_2->addWidget(pushButton);
}
As pnezis wrote, you probably want to override the default minimum size calculated by the button. Here's a way you can do it while avoiding to choose an arbitrary size that might not work when conditions vary (different font or font size, UI style, etc):
QWidget* parent = /* some widget */
auto button = new QPushButton(QLatin1String("X"), parent);
auto textSize = button->fontMetrics().size(Qt::TextShowMnemonic, button->text());
QStyleOptionButton opt;
opt.initFrom(button);
opt.rect.setSize(textSize);
button->setMinimumSize(
button->style()->sizeFromContents(QStyle::CT_PushButton,
&opt,
textSize,
button));
The above was adapted and simplified from QPushButton's own code. You may want to look at the source of QPushButton::sizeHint for all the details.
setMaximumWidth works for me.
sample code is in pyqt, but it should translate directly to C++ without any problems.
from PyQt4 import QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QtGui.QHBoxLayout()
texts = [":)",
"&Short",
"&Longer",
"&Different && text",
"More && text",
"Even longer button text", ]
for text in texts:
btn = QtGui.QPushButton(text)
double = text.count('&&')
text = text.replace('&', '') + ('&' * double)
width = btn.fontMetrics().boundingRect(text).width() + 7
btn.setMaximumWidth(width)
layout.addWidget(btn)
self.setLayout(layout)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
mainWin = Window()
mainWin.show()
sys.exit(app.exec_())