Qt QTableView 1 line vertical size - qt

I have the following to add a QTableView to a QWidget:
QVBoxLayout *vLayout = new QVBoxLayout(this);
QTableView *tableView = new QTableView;
tableView->horizontalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setVisible(false);
vLayout->addWidget(tableView);
This widget will use model which load data from MySQL... And there is only one line of content, so I would like to make the view just height enough to show one line. How to approach this issue?

I had the same problem and found that this works best:
const auto height = table.horizontalHeader()->sizeHint().height() + table.rowHeight(0);
table.setMinimumHeight(height);
table.setMaximumHeight(height);
You force the table to keep the exact size specified by you: the header size + the size of a single row.

Related

Image scaling is ignored when adding QLabel with image at runtime

I am trying to add a QLabel with an image to my GUI at runtime, but the scaling is ignored and the image expands to its full size (which is larger than the screen), ignoring the size constraints and not scaling the contents correctly.
The image should be fit into the bottom, left side of the window, as my GridLayout describes here:
headerPnl= new HeaderPnl();
buttonPnl = new ButttonPnl;
mainContentPnl = new QStackedWidget;
mainLayout = new QGridLayout;
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
mainLayout->addWidget(headerPnl, 0, 0, 1, 7);
mainLayout->addWidget(mainContentPnl, 1, 0, 10, 6);
mainLayout->addWidget(buttonPnl, 1, 6, 10, 1);
mainLayout->setRowStretch(0,1);
mainLayout->setRowStretch(1,2);
mainLayout->setRowStretch(2,2);
mainLayout->setRowStretch(3,2);
mainLayout->setRowStretch(4,2);
mainLayout->setRowStretch(5,2);
mainLayout->setRowStretch(6,2);
mainLayout->setColumnStretch(0,1);
mainLayout->setColumnStretch(1,1);
mainLayout->setColumnStretch(2,1);
mainLayout->setColumnStretch(3,1);
mainLayout->setColumnStretch(4,1);
mainLayout->setColumnStretch(5,1);
mainLayout->setColumnStretch(6,1);
this->setLayout(mainLayout);
The header goes across the top, the button panel goes along the right side, and the rest of the screen is changing depending on the workflow of the application (ie what buttons are pressed, etc).
When necessary, my GUI replaces the widgets and updates the GUI like this:
void MainWindow::setContentPane(QWidget *content){
mainLayout->replaceWidget(contentPnl, content);
contentPnl = content;
}
void MainWindow::setButtonPanel(QWidget *buttonPanel){
mainLayout->replaceWidget(buttonPnl, buttonPanel);
buttonPnl = buttonPanel;
}
void MainWindow::configureWelcome(){
QLabel *welcomeLbl = new QLabel;
welcomeLbl->setObjectName("welcomeLbl");
welcomeLbl->setPixmap(QPixmap(":/images/welcome.jpg"));
welcomeLbl->setScaledContents(true);
CustomWidget *welcomeWidget = new CustomWidget;
QHBoxLayout *welcomeLayout = new QHBoxLayout;
welcomeLayout->addWidget(welcomeLbl);
welcomeWidget->setLayout(welcomeLayout);
setContentPane(welcomeWidget);
CustomWidget *buttonPnl = createWelcomeButtonPanel();
setButtonPanel(buttonPnl);
}
How can I make this image fit inside the GridLayout properly? It seems like when adding widgets to a layout that has already been set, the GUI doesn't know how to handle the size constraints from the GridLayout. Replacing the buttons works fine, but adding an image does not.
Side question: I have been trying to stay away from a QStackedWidget, as this application is designed for a lower power system, and it doesn't make sense to me to create all the possible screens and add them all to a QStackedWidget when the application starts. Instead I would rather use the resources when necessary, and only create all the GUI elements when I need to (ie, when the right buttons are clicked). Does that make sense?
Did you had a QSizePolicy to the widget containing the QGridLayout? I suggest an horizontal and vertical QSizePolicy::Fixed.
In your first code segment add :
this->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

How can I remove spacing around a widget in a layout?

I am trying to insert a very tiny QLabel into a very tiny QFrame.
The QFrame is used as a spacer and I thought it a perfect place to add text.
In code that text would change (in to mm). Because mm is so giant it is clipped...
Yet it seems to me it would fit if I could just get rid of the margins !
So I try:
ui->tinyFrame is created in Designer... it has geometry 0,0,10,10
QLabel* unitLabel = new QLabel("mm");
unitLabel->setFixedSize(8,8);
unitLabel->setFont(QFont("Arial Narrow", 7));
unitLabel->setMargin(0);
unitLabel->setContentsMargins(0,0,0,0);
QHBoxLayout* unitLayout = new QHBoxLayout();
unitLayout->setSpacing(0); // I try everything
unitLayout->setMargin(0);
unitLayout->setContentsMargins(0,0,0,0);
ui->tinyFrame->setLayout(unitLayout);
ui->tinyFrame->setContentsMargins(0,0,0,0);
unitLayout->addWidget(unitLabel);
What else can I try to remove the space around my little label ?
I am trying to insert a very tiny QLabel into a very tiny QFrame.
What can I try to remove the space around my little label?
QLabel is derived from QFrame: maybe you can just replace that QFrame with QLabel then? And no nested layout for that frame.
... as long as you tried layout->setSpacing(0) already...

Fill the QLabel/QGraphics widget placed in the grid with the image

I have placed two QGraphicsView's and one QLabel inside a horizontal layout (QHBoxLayout) with its layoutStretch set to 1, 1, 1. The problem is when I try to load images inside them, the images does not fill the widgets area. Here is the code:
QPixmap pix1("image1.jpg");
pix1 = pix1.scaled(ui->label1->size());
ui->label1->setPixmap(pix);
QPixmap pix2("image2.jpg");
pix2 = pix2.scaled(ui->graphicsView1->size());
ui->graphicsView1->scene()->addPixmap(pix2);
QPixmap pix3("image3.jpg");
pix3 = pix3.scaled(ui->graphicsView2->size());
ui->graphicsView2->scene()->addPixmap(pix3);
And here is the undesired output:
I have tried setting HorizontalPolicy and VerticalPolicy property of widget to Expanding and also Minimum, but none of them helped either.
Set size policy to QSizePolicy::Ignored and scale with Qt::KeepAspectRatioByExpanding:
ui->label1->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
pix1 = pix1.scaled(ui->label1->size(), Qt::KeepAspectRatioByExpanding);
ui->label1->setPixmap(pix);
You probably want to scale pixmaps in resizeEvent().

How to set the size of a QGraphicsView?

I'd like to have a fixed size QGraphicsView, which I want to add to a layout together with some other widgets. However, the QGraphicsView simply ignores resize(), here is the relevant code:
QGraphicsScene* scene = new QGraphicsScene;
QGraphicsView* view = new QGraphicsView(scene);
view->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
view->resize(1000, 600);
QVBoxLayout* layout = new QVBoxLayout;
layout->setMargin(0);
layout->addWidget(view);
setLayout(layout);
If I use setFixedSize() instead of resize(), the size is actually being set correctly. However, it seems that the window size is not updated, the window is not centered properly.
How am I supposed to set a fixed size for a QGraphicsView?
I know this is a very old question, but in case anyone else stumbles into it: you can set both minimumSize and maximumSize to the desired target size, and it should work regardless of what layout the QGraphicsView is in.
If I understood you right you want to have QGraphicsView centred inside the window and having fixed size. You need rather then VBoxLayout you should use QGridLayout with spacers, so your form should look like:
<Empty> <VSpacer> <Empty>
<HSpacer> <GraphicsView> <HSpacer>
<Empty> <VSpacer> <Empty>

Two QTableViews in QLayout

I want to expand QTableViews' size so each QTableView takes the half size of the window! , how can i do it?
screenshot:
http://i.stack.imgur.com/Rh87o.jpg
Add a QHBoxLayout to your widget/window and then add the tables to it.
The following code on a top level widget..
QHBoxLayout *horizontalLayout;
QTableView *tableView;
QTableView *tableView_2;
horizontalLayout = new QHBoxLayout(Widget);
tableView = new QTableView(Widget);
horizontalLayout->addWidget(tableView);
tableView_2 = new QTableView(Widget);
horizontalLayout->addWidget(tableView_2);
Will give you something like..
The two tables share the available space equally and expand and resize with the main window.

Resources