Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a QDialog window where a bar to input text is shown, together with two buttons (one for accept and one for cancel), and the top instruction "Please, provide ID...". At the same time I have a warning/error message which is hidden by default.
The window is something like this:
The problem is that when I call the show() to display the warning, it does not rescale the window, so the warning is shown on top the rest of the input bar. As seen below:
I have been looking at some properties like sizePolicy or even using adjustSize, but I have not been able to make for example the top instruction just move a bit to the top when the warning is displayed so they do not stack on top of each other.
I image there should be an easy way of managing this, could someone point out what am I missing or what should I change?
The version of Qt that I am using is Qt 5.4
All elements must be in the layout. The warning has not been added to the layout, and thus when it's shown it retains its original position.
For reference, the dialog could be written approximately as follows:
class IdDialog : public QDialog {
QGridLayout m_layout{this};
QLabel m_header{tr("Enter an ID")};
QLabel m_warning{tr("<font color=\"red\">Subject not found.</font>")};
QLineEdit m_subject;
QDialogButtonBox m_box;
QPushButton *const m_disconnect = m_box.addButton(tr("Disconnect"), QDialogButtonBox::RejectRole);
QPushButton *const m_confirm = m_box.addButton(tr("Confirm"), QDialogButtonBox::AcceptRole);
public:
IdDialog(QWidget *parent = nullptr) : QDialog(parent)
{
m_layout.addWidget(0, 0, &m_header);
m_layout.addWidget(1, 0, &m_warning);
m_layout.addWidget(2, 0, &m_subject);
m_layout.addWidget(3, 0, &m_box);
m_warning.hide();
// connect signals here, etc.
}
};
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to make responsive app where all the content get resize according to MainWindow as I increase/decrease its size (responsive webpage like interface).
please consider my problem..
You have to add a layout to your widget or main window. All widgets added to this layout will resize according to their size policy.
1. Create the layout
In the QtCreator/designer
Create a new widget or window from the template. Afterwards drag a layout from the left into your widget/window.
Then you have to tell the layout to use all available space in the widget/window (popup menu on the right).
All widgets you add to this layout will be resized by the layout, which fills the whole space in the widget/window.
Programmatically
main.cpp
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyMainWindow *win = new MyMainWindow();
win->show();
return app.exec();;
}
MyMainWindow.h
class MyMainWindow: public QMainWindow {
public:
MyMainWindow(QMainWindow *parent = Q_NULLPTR);
};
MyMainWondow.cpp
MyMainWindow::MyMainWindow(QMainWindow *parent): QMainWindow(parent) {
// replace the central widget to set our own layout
QWidget *centralWidget = new QWidget(this);
QVBoxLayout *verticalLayout = new QVBoxLayout(centralWidget);
centralWidget->setLayout(verticalLayout);
setCentralWidget(centralWidget);
// add widgets here, for testing a table, that uses all available space
verticalLayout->addWidget(new QTableView);
}
2. Adjust size policy
For custom widgets inside a layout you may have to set the size policy to tell the widget/layout how to resize the widget. See QSizePolicy and setSizePolicy. All Qt Widgets should have reasonable size policies by default (e.g. tables expand, while buttons do not).
Example:
To make a widget take as much space as possible it needs to have the QSizePolicy::Expanding set.
This property can also be set in the QtDesigner/Creator.
// set for horizontal and vertical at the same time
myWidget->setSizePolicy(QSizePolicy::Expanding);
// set different values for horizontal and vertical
myWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
verticalLayout->addWidget(myWidget);
This question already has an answer here:
QScrollArea with dynamically changing contents
(1 answer)
Closed 6 years ago.
I'm using Qt5 on Windows7.
In my current app I try to display and remove a number of push-buttons.
widget = new ButtonWidget(ui->frame); // frame is a QScrollArea
connect(ui->addBtns, SIGNAL(clicked()), widget, SLOT(addButtons()));
connect(ui->deleteBtns, SIGNAL(clicked()), widget, SLOT(deleteButtons()));
And the ButtonWidget class is here:
ButtonWidget::ButtonWidget(QWidget * parent) : QWidget(parent)
{
gridLayout = new QGridLayout(parent);
}
static QStringList texts{...}; // various content (see explanations below)
void ButtonWidget::addButtons()
{
for(auto i = 0; i < texts.size(); i++)
{
gridLayout->addWidget(new QPushButton(texts[i], this), i / 5, i % 5);
}
}
void ButtonWidget::deleteButtons()
{
while(gridLayout->count())
{
delete gridLayout->itemAt(0)->widget();
}
}
If I set QStringList texts{"1\nok"}; I get this:
...which is ugly (centered and expanded horizontally on all layout).
If I set QStringList texts{"1\nok",...,"24\nok"}; I get this:
...which is/seems quite ok.
And finally, if I set QStringList texts{"1\nok",...,"36\nok"}; I get this:
...which is very bad, garbled, etc.
So, the question: Is there any way to fix this, i.e. to disable somehow this "shrink-to-fit" on the layout? A default button size would be just fine.
I would like to take advantage of the vertical-scroll feature of the QScrollArea, not to stuff and cram all the buttons in the (limited) available space...
If you merely add child widgets to a QScrollArea, you won't get any scrolling, since these widgets are not managed by the area in any way. You need to set the widget using QScrollArea::setWidget instead:
widget = new ButtonWidget;
ui->frame->setWidget(widget);
So, the question: Is there any way to fix this, i.e. to disable
somehow this "shrink-to-fit" on the layout? A default button size
would be just fine.
Of the top of my head, one can set a minimum size per button. One can calculate what the minimum size should be from the font (but first just play with setting the minimum size).
Here is a reference. You will notice that minimumSize takes precedence over layout. It might also change the SizePolicy, which you can also look at. I suggest you play with the designer, as it respects sizing and layouts. One can learn allot from it.
Also look at minimumSizeHint, as the layout interacts with this. See also MasterBLB's comments here
I have a small problem with QT layout.
I have a toolbox, and I want to populate it with some checkable buttons with a description. So I create a QWidget with a QGridLayout and put the QButton in the first cell and the QLabel in the second.
This is the most important part of the code (I removed dependancies from other irrelevant code in the app):
QWidget *createCellWidget()
{
QToolButton *button = new QToolButton(this);
button->setCheckable(true);
button->setMinimumSize(57,57);
button->setMaximumSize(57,57);
QWidget *widget = new QWidget(this);
QGridLayout *layout = new QGridLayout(widget);
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
QLabel *lbl = new QLabel("my very long caption");
lbl->setWordWrap(true);
lbl->setAlignment(Qt::AlignCenter);
layout->addWidget(lbl, 1, 0, Qt::AlignTop);
widget->setMaximumWidth(80);
widget->setMinimumWidth(80);
return widget;
}
then I create a QGridLayout and populate it with these controls:
QWidget *itemWidget_FlowControl = new QWidget(this);
QGridLayout *_flowControl_layout = new QGridLayout(itemWidget_FlowControl);
_flowControl_layout->addWidget(createCellWidget(), 0, 0);
This works well and produces this output:
this is a nice layout. Unluckily if I enlarge the window the controls do not "flow", so I tried to replace the QGridLayout with a flowlayout (here are the source files).
Now the behavior is much better. BUT...
this is what I get. The longer captions are laid out as if they were single-lined, so the text overlaps with the button.
What can I do to make it look like before, but keeping it as a "flow layout"? Or do you know if there is any alternative in QT 5.2?
Thank you
What's wrong?
You are on the right track of using flowlayout.
The only problem is the internal layouts (QGridLayout in your case) of your cell widgets also stretch in response to the resize events.
Solution
The solution is surprisingly simple:
Try to limit the stretch of the internal layout.
Within the factory function QWidget *createCellWidget():
[Option 1]
Add lbl->setMaximumWidth(60); to manually limit the stretch of label width. This makes the internal layout not stretch so "freely."
[Option 2]
Add layout->setSizeConstraint(QLayout::SetFixedSize); to restrain the internal layout stretch. By doing this, you may want to manually add some line break code (\n) to your "very long caption" in case the label width automatically decided by Qt doesn't suit your need.
Result
IMO the best approach would be use off style sheets (flow layout is Ok).
Simply use QButton but customize its view by altering its stye sheet.
Currently I have no time to write an example.
I want to do universal method to set position of my widget.
All I wanna get it is set right coordinates for my widget wich must always be in right bottom corner of desktop. My widget can change his height (or maybe width) but it must have adjusted size by both ordinates... (too many words)
My idea is using QDesktopWidget as basic widget to put into my QLayout with stratch items (to align inner (my) widget to right and to bottom sides)
my code prototype is here:
QDesktopWidget * desktopWidget = QApplication::desktop();
MyWidget * myWidget = new MyWidget(desktopWidget);
QVBoxLayout * vlayout = new QVBoxLayout;
vlayout->addStretch();
vlayout->addWidget(myWidget);
QHBoxLayout * hlayout = new QHBoxLayout(desktopWidget);
hlayout->addStretch();
hlayout->addLayout(vlayout);
but it doesn't work...
Help me please implement my idea if you know how.
At this moment I know only one work way to do it - it is manually set pos of widget and handle many events (resize etc.) - but this is not good... (because i do it bad of cause ;-)
)
PS: idea with qlayout inside other widget is working for example with QTextBrowser with sandclock at certer of view, etc.
A QDesktopWidget isn't intended to be used like a typical widget (at least as far as I'm aware, I'm surprised the documentation isn't more explicit about that). So you shouldn't try to parent widgets to it or try to assign it a layout. You call its methods to obtain information about the desktop environment or connect to its signals to be informed of changes.
Using this information, you would then set the geometry of your own application widgets so that they appear on the correct screen and position you want.
This page shows some basic functionality.
I have a QListView in Icon mode with lots of icons, so that a scrollbar appears, but the scrolling is not smooth and this IMHO confuses the user since it jumps abruptly from one point to another at each scroll. I would like to make the scrolling smooth but I didn't find anything in the docs. Is it possible?
Maybe QListView.setVerticalScrollMode(QAbstractItemView::ScrollPerPixel)
If I understand your question correctly you would like to redefine the scrolling behavior of the widget. I guess what happens is that listview is getting scrolled by the item's height whenever users hits a scroll arrow (marked as b on the image below).
For a vertical scroll bar connected to a list view, scroll arrows typically move the current position one "line" up or down, and adjust the position of the slider by a small amount. I believe line in this case it is an icon's height. You can adjust items height by installing and item delegate (setItemDelegate) and overriding its sizeHint method. Though this would not help you to solve this problem. What you could try is to create a QListView descendant and override its updateGeometries method. There you can setup the vertical scrollbar step to the value you want, I guess 1 or 2 for this task. Below is an example of the custom listview:
class TestListView : public QListView
{
Q_OBJECT
public:
explicit TestListView(QWidget *parent = 0);
protected:
virtual void updateGeometries();
};
TestListView::TestListView(QWidget *parent) :
QListView(parent)
{
//???
}
void TestListView::updateGeometries()
{
QListView::updateGeometries();
verticalScrollBar()->setSingleStep(2);
}
hope this helps, regards
I have a QlistWidget* in ui->barra_scroll and I feel very smooth with this.
QScrollBar *qsb = ui->barra_scroll->verticalScrollBar();
qsb->setSingleStep(5);