In Qt, how to resize icons in a table? - qt

I put icons as items in a table:
QTableWidget *table = new QTableWidget(this);
QTableWidgetItem *item = new QTableWidgetItem;
item->setSizeHint(QSize(100, 100));
item->setIcon(QIcon(fileName));
table->setItem(0, 0, item);
However no matter the icons' size, they are shown extremely small in the table.
I do not care about the text.
How can I get them bigger?

You need to change the size of the icons in the QTableWidget. You can do so using the iconSize property inherited from QAbstractItemView. See here.
QTableWidget *table = new QTableWidget(this);
table->setIconSize(QSize(100, 100));
QTableWidgetItem *item = new QTableWidgetItem;
item->setSizeHint(QSize(100, 100));
item->setIcon(QIcon(fileName));
table->setItem(0, 0, item);

Related

Space Buttons equally in Layout, but with an empty slot

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...

Adding label to video

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.

How to delete a space between QLabel and QLineEdit

I want to reduce space between label and QLineEdit (QLabel is above QLineEdit). How can I achieve it? In the code I'm creating items, that I later put in some layouts.
QLabel* lgamma = new QLabel("Gamma");
gamma = new QLineEdit();
QLabel* lmin_linie = new QLabel(QString::fromUtf8("Min. il. zmian linii"));
min_lin = new QLineEdit();
// ...
QLabel* lmax_kursy = new QLabel(QString::fromUtf8("Max zm. il. kursów"));
max_kursy = new QLineEdit();
QGridLayout *lay = new QGridLayout(this);
QVBoxLayout *box1 = new QVBoxLayout();
QVBoxLayout *box2 = new QVBoxLayout();
// ...
QVBoxLayout *box12 = new QVBoxLayout();
box1->addWidget(lmin_linie);
box1->addWidget(min_lin);
box2->addWidget(lmax_lin);
box2->addWidget(max_lin);
// ...
box12->addWidget(literacje);
box12->addWidget(iteracje);
verticalColumn1->addLayout(box1);
verticalColumn1->addLayout(box2);
// ...
verticalColumn3->addLayout(box12);
start = new QPushButton("Start", this);
QHBoxLayout *corn = new QHBoxLayout();
corn->addLayout(verticalColumn1);
corn->addLayout(verticalColumn2);
corn->addLayout(verticalColumn3);
QVBoxLayout *rup = new QVBoxLayout();
rup->addLayout(corn);
rup->addWidget(start);
You can simply add a spacer to your layout.
QSpacerItem *spacer = new QSpacerItem(1, 50, QSizePolicy::Ignored, QSizePolicy::Expanding);
box1.addItem(spacer);
Adapt the args or QSpacerItem for your needs, for examle QSizePolicy::Preferred could be better than QSizePolicy::Expanding, and reduce the preferred height (second argument).
Why don't you use the easy way to do that GUI with Qt designer ?
You can try to set QLabel and QLineEdit border:
lmax_kursy->setStyleSheet("border-width:0px");
max_kursy->setStyleSheet("border-width:0px");
or set spacing in layout. First check what is a current value of spacing:
box1->spacing();
If it's 0, try to set negative value like -2:
box1->setSpacing(-2); // or 0 or something else

QTextBrowser Resizing on a QGridLayout

I have a QGridLayout which contains all the layout for my class. Until there everything goes fine.
I added a QSplitter for other things and then at the bottom (After the splitter) i want the QTextBrowser to be. Fine, works. But i want to resize, i meant i want to make the QTextBrowser smaller. But resize doesn't work.
QGridLayout *layout = new QGridLayout(this);
QSplitter *splitter = new QSplitter(Qt::Horizontal);
text1 = new QPlainTextEdit();
text2 = new QPlainTextEdit();
splitter->addWidget(text1);
splitter->addWidget(text2);
text1->resize(800, this->height());
layout->addWidget(splitter, 1, 0);
browser = new QTextBrowser();
browser->resize(1, 1);
layout->addWidget(browser, 2, 0);
setLayout(layout);
Actually resizing the text1 works fine, but i can't make the QTextBrowser resize fine. Any idea?

QScrollArea not respecting contentMargins setting

QScrollArea, for some reason, is ignoring the contentMargins setting when I set QGraphicsView as its widget. Looking at the snippet below, can someone please tell if I'm doing something wrong or it could be a bug in the SDK?
Snippet 1 (works perfect):
QWidget *appWindow = new QWidget;
QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);
QWidget *widgetToScroll = new QWidget(sa);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);
QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);
appWindow->show();
Snippet 2 (It's like setContentMargins() call is ignored completely):
QWidget *appWindow = new QWidget;
QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);
QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);
appWindow->show();
Thanks.
To make the content margins work properly for a QScrollArea widget I subclass it and manually set the viewport margins (which is a protected method in QT 4.7)
// Extended class
class QScrollAreaWithMargins : public QScrollArea
{
public:
virtual void resizeEvent(QResizeEvent *event) override
{
// Define content margins here
setViewportMargins(5, 0, 0, 0); // <<<<< SET MARGINS HERE
QScrollArea::resizeEvent(event);
}
};
// Usage
//...
mEditorScrollArea = new QScrollAreaWithMargins();
//...
It looks like you are confusing the structure of how to nest a QGraphicsView and a QGraphicsScene. (Maybe it was just a typo?)
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
should be changed to
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa);
or
QGraphicsView *widgetToScroll = new QGraphicsView();
sa->setWidget(widgetToScroll);
When you add a QWidget to a layout, you change the widget's parent. When you set a widget (or QGraphicsView) to a QScrollArea, you change that widget's parent. See Object Trees & Ownership for more information. So if you wanted to set up your QGraphicsView inside a QScrollArea your code would look like this:
QWidget *appWindow = new QWidget;
QScrollArea *sa = new QScrollArea(); // No need to specify a parent here if
// you add it to a layout later
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentsMargins(50, 50, 50, 50);
QGraphicsView *widgetToScroll = new QGraphicsView();
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll); // This sets the parent for widgetToScroll
QVBoxLayout *appWindowLayout = new QVBoxLayout();
appWindowLayout->addWidget(sa); // This sets the parent for sa
appWindow->setLayout(appWindowLayout); // This sets the parent for appWindowLayout
appWindow->show();
As a side note...
When using QGraphicsViews with a QGraphicsScene, instead of setting the margins using a QScrollArea's setContentsMargins, I use the QGraphicsView automatic scrolling and just set the scene rect to have a larger margin that the size of my content like so:
QWidget *appWindow = new QWidget;
QGraphicsView *widgetToScroll = new QGraphicsView();
QGraphicsScene *scene = new QGraphicsScene();
scene->addRect(0,0, 5000, 5000);
widgetToScroll->setSceneRect(-50,-50, 5050, 5050);
widgetToScroll->setScene(scene);
QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(widgetToScroll);
appWindow->setLayout(appWindowLayout);
appWindow->show();
The QGraphicsView includes quite a bit more than just automatic scrolling when needed. You can resize everything inside of it and quite a bit more. It is great for 2D layouts, interactions and animations. See Qt's Graphics View Framework at http://doc.qt.io/qt-5/graphicsview.html for more information.
Here is more information that may be useful when using margins and paddings: The Box Model used by QStyleSheets.

Resources