I'm trying to create a panel of buttons that will have 4 buttons, a space, and another button, all of equal space, like this:
I have tried to use Spacers, but it seems like those require a specific height and weight, and I would like this layout to be dynamic enough to appear correctly on any resolution, so a fixed size Spacer would not work.
I have tried to following code, but this just squishes the first 4 buttons to the top and the last one to the bottom, and doesn't space them out evenly.
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(15);
layout->setSpacing(15);
layout->addWidget(button1, 1);
layout->addWidget(button2, 1);
layout->addWidget(button3, 1);
layout->addWidget(button4, 1);
layout->addWidget(button5, 2, Qt::AlignBottom);
layout->addStretch();
buttonPnl->setLayout(layout);
I also tried using a QGridLayout and specifying the height of each row, but this looks the same as the previous example.
QGridLayout *gridLayout = new QGridLayout;
gridLayout->setMargin(15);
gridLayout->setSpacing(15);
gridLayout->addWidget(button1, 0, 0);
gridLayout->addWidget(button2, 1, 0);
gridLayout->addWidget(button3, 2, 0);
gridLayout->addWidget(button4, 3, 0);
gridLayout->addWidget(button5, 5, 0);
gridLayout->setRowStretch(0, 1);
gridLayout->setRowStretch(1, 1);
gridLayout->setRowStretch(2, 1);
gridLayout->setRowStretch(3, 1);
gridLayout->setRowStretch(4, 1);
gridLayout->setRowStretch(5, 1);
How can I create a dynamic layout that will display my buttons correctly at any reasonable resolution?
It's a little bit ``hacky-slash'' but... the easiest way to get the desired behaviour is probably to define a spacer class that inherits from QPushButton but has an empty paintEvent definition...
class spacer: public QPushButton {
using super = QPushButton;
public:
using super::super;
protected:
virtual void paintEvent (QPaintEvent *event) override
{
}
};
Then just make sure you instantiate it with a text string that's in keeping with the other buttons so that it has a suitable return value from sizeHint(). So (based on your own example)...
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
/*
* Add a spacer using the text from button4 as a reference.
*/
layout->addWidget(new spacer(button4->text()));
layout->addWidget(button5);
layout->addStretch();
buttonPnl->setLayout(layout);
This gives me something like...
Related
I would like to add push buttons to the layout. The newest item would be on the top of the layout.
I also would like position the buttons to the top, thus I am using QSpacerItem.
Here is what I have tried so far.
Constructor:
//frame is a QFrame
lVertical = new QVBoxLayout(frame); //private variable
lVertical->setMargin(0);
lVertical->setSpacing(0);
auto verticalSpacer = new QSpacerItem(10, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
lVertical->addItem(verticalSpacer);
connect(b, &QPushButton::clicked, this, &MainWindow::addToLayout);
Function:
void MainWindow::addToLayout() {
QPushButton* button = new QPushButton(frameSlider);
button->setText(QString::number(i));
++i; //private variable
layoutVertical->addWidget(button);
}
Currently I add like this:
But I would like to add like this:
The problem is that you have placed a spacer at the beginning so it will stretch all the widgets down.
One possible solution is to add a stretch and then insert an element before, You should not use QSpacerItem:
// constructor
layoutVertical = new QVBoxLayout(frame);
layoutVertical->setMargin(0);
layoutVertical->setSpacing(0);
layoutVertical->addStretch();
void MainWindow::addToLayout() {
QPushButton* button = new QPushButton();
layoutVertical->insertWidget(0, button);
}
I have to write a simple video player that can display some subtitles,link or a picture(like on YouTube) in a certain time. I have no idea of how do display anything using QVideoWidget. I couldn't find any useful class to do it. Could you please give me some advices?
I did It your way but after i load any video QLabel disappears...
player->setVideoOutput(vw);
playlistView->setMaximumWidth(200);
playlistView->setMinimumWidth(300);
window = new QWidget;
Playerlayout = new QGridLayout;
subtitleWidget = new QLabel;
subtitleWidget->setMaximumWidth(1000);
subtitleWidget->setMaximumHeight(100);
subtitleWidget->setStyleSheet("QLabel {background-color : red; color
blue;}");
subtitleWidget->setAlignment(Qt::AlignCenter | Qt::AlignBottom);
subtitleWidget->setWordWrap(true);
subtitleWidget->setText("example subtitle");
Playerlayout->addWidget(vw,0,0);
Playerlayout->addWidget(subtitleWidget,0,0);
Playerlayout->addWidget(playlistView,0,1,1,2);
If QVideoWidget doesn't provide what you require directly then you could always set up an overlay.
The basic layout item hierarchy would be something like...
QWidget
layout
QVideoWidget
subtitle_widget
In this case the layout could be either a QStackedLayout using stacking mode QStackedLayout::StackAll or a QGridLayout with both the QVideoWidget and the subtitle_widget occupying the same cells but with the correct z-order.
Going with the QGridLayout...
auto *w = new QWidget;
auto *l = new QGridLayout(w);
auto *video_widget = new QVideoWidget;
auto *subtitle_widget = new QLabel;
/*
* Subtitles will be shown at the bottom of the 'screen'
* and centred horizontally.
*/
subtitle_widget->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
subtitle_widget->setWordWrap(true);
/*
* Place both the video and subtitle widgets in cell (0, 0).
*/
l->addWidget(video_widget, 0, 0);
l->addWidget(subtitle_widget, 0, 0);
Subtitles etc. can now be displayed simply by invoking subtitle_widget->setText(...) at the appropriate time.
The same method can easily be extended to overlaying other types of information.
I want to design a GUI using Qt. That contains lines and check-boxs, which are connected to each other like this :
----------[ ]-----------
------[ ]---------[ ]-------------
(where dash represents line and [] is for check-box)
Lines are created dynamically. And selecting the check-box will disable the corresponding line. So basically the lines and check-box should be on same layer.
Any hint/link about the implementation is appreciated.
You'll need a combination of QFrame, QCheckBox, and QHBoxLayout. For something a little fancier, you could sub-class your own QWidget for each section and add them incrementally to a QVBoxLayout. Something like this...
class CheckLine : public QWidget
{
Q_OBJECT
public:
CheckLine(int numboxes = 1, QObject* parent = 0) :
QWidget(parent)
{
m_layout = new QHBoxLayout;
m_layout->setSpacing(0); //you can also set the margins to zero if need be
setLayout(m_layout);
QFrame* firstline = new QFrame();
firstline->setFrameShape(QFrame::HLine);
m_layout->addWidget(firstline);
m_lines.append(firstline);
for(int i = 0; i < numboxes; ++i)
addBox();
}
void addBox()
{
QCheckBox* newbox = new QCheckBox(""); //add text here - or leave it blank if you want no label
m_newbox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_layout->addWidget(newbox);
m_boxes.append(newbox);
QFrame* newline = new QFrame();
newline->setFrameShape(QFrame::HLine);
m_layout->addWidget(newline);
m_lines.append(newline);
/* link each checkbox to disable the line after it */
connect(newbox, SIGNAL(toggled(bool)), newline, SLOT(setEnabled(bool)));
// connect(newbox, SIGNAL(toggled(bool)), this, SLOT(setEnabled(bool))); //use this instead if you want it to disable the entire row
}
private:
QHBoxLayout* m_layout;
QList<QCheckBox*> m_boxes;
QList<QFrame*> m_lines;
};
Then, create a widget with a QVBoxLayout and new a CheckLine, incrementing numboxes by 1 each time. Tweak the code if you want any checkbox to disable the entire line.
I have the following:
QGridLayout *layout = new GridLayout();
layout->addWidget(rightBuf, row, 0);
layout->addWidget(i, row, 1);
layout->addWidget(leftBuf, row, 2);
Which displays fiine. However, when I add the alignment to the second line, ie:
layout->addWidget(i, row, 1, Qt::AlignTop);
The widget disappears from the screen. I think I have tried about just everything to get it to align top, including putting it into a QVBoxLayout and then adding it to the QGridLayout. Is there anything missing here?
I have a QFrame derived object:
class SubjectLineDisplay : public QFrame
{
Q_OBJECT
private:
// Members
public:
explicit SubjectLineDisplay(const QString&, const QString&, quint32, QWidget *parent = 0);
};
In the constructor, I set a background for it and a border:
QPalette p(palette());
p.setColor(QPalette::Background, QColor(255, 255, 255));
setPalette(p);
setLayout(mainLayout); // The mainLayout is a VBoxLayout which is a collection of a few QLabels
setFixedHeight(lTitle->size().height() + lId->size().height());
When i do this in main():
SubjectLineDisplay* x = new SubjectLineDisplay("NETWORK", "Network Centric Programming", 4);
x->show();
The widget shows up in a window, with the background and frame displaying properly, just as I would want. However, when I add it to another layout to show it:
SubjectLineDisplay* lineDisplay = new SubjectLineDisplay(
subjectNameLE->text(), idLE->text(), creditSpin->value()
);
emit newSubjectAdded(Course(subjectNameLE->text(), idLE->text(), creditSpin->value()));
subjectNameLE->clear();
creditSpin->setValue(3);
idLE->clear();
subjectLineLayout->addWidget(lineDisplay); //Adding the widget to a layout
Now, I don't see the frame or the border. How do I get the layout to display the frame and the border? What am I doing wrong?
Could you try using setAutoFillBackground(true)?
For as far as I'm aware foregrounds are always drawn, but backgrounds are not.