Как изменить размер QTableWidget, чтобы он не занимал все пространство? - PullRequest
0 голосов
/ 04 июля 2019

Я сделал графический интерфейс с Qt5:

enter image description here

Но стол в правом нижнем углу растягивает "Pièce" QGroupBox справа. Я изменил размер таблицы, прежде чем добавить ее в макет "Pièce".

Потому что, если я не изменяю размер таблицы, у меня есть этот графический интерфейс: enter image description here

Вот интерфейс без таблицы:

enter image description here

Я попытался обновить макет «Pièce» после добавления своей таблицы, обновить свою таблицу перед добавлением ее в макет и также обновить метку «Клиент: Н / Д».

// Return the "Pièce" QGroupBox
static QGroupBox* ajout_cadre_administratif(QWidget& page)
{
    QGroupBox* cadre_administratif = new QGroupBox(nom_section_piece);
    QVBoxLayout* support_principal = new QVBoxLayout(cadre_administratif);

    QHBoxLayout* support_client = ajouter_champs(titre_client.toStdString(), "N/A");
    support_principal->addLayout(support_client);

    QTableWidget* tableau = creation_tableau_references();
    support_principal->addWidget(tableau);

    support_client->update();

    support_principal->update();
    tableau->show();
    return cadre_administratif;
}

// Create the table and return it
static QTableWidget* creation_tableau_references()
{
    QTableWidget* table = new QTableWidget(0, 1);
    initialisation_tableau(*table);

    ajouter_element_tableau("STA-J915637925-B", *table);
    ajouter_element_tableau("STA-J015837049-A", *table);
    ajouter_element_tableau("STA-J915634923", *table);
    /*ajouter_element_tableau("STA-J965536925-B", *table);
    ajouter_element_tableau("STA-J915000001-B", *table);*/

    table->setFixedSize(taille_optimale_tableau(*table, 3));
    table->update();
    return table;
}

// add an item to the the table named "tableau"
void ajouter_element_tableau(const std::string& element, QTableWidget& tableau)
{
    QTableWidgetItem* item = new QTableWidgetItem(element.c_str());
    tableau.insertRow(tableau.rowCount());
    tableau.setItem(tableau.rowCount() - 1, 0, item);
}

// Init the table named "tableau"
static void initialisation_tableau(QTableWidget& tableau)
{
    tableau.horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    tableau.setEditTriggers(QAbstractItemView::NoEditTriggers);
    QStringList text(entete_tableau_references_piece);
    tableau.setHorizontalHeaderLabels(text);
    tableau.verticalHeader()->setVisible(false);
    tableau.setColumnCount(1);
}

// return the optimum size of the table
const QSize taille_optimale_tableau(QTableWidget& table, const short int nombre_lignes_maximum)
{
    int width = largeur_optimal(table, nombre_lignes_maximum);
    int height = hauteur_optimale(table, nombre_lignes_maximum);
    return QSize(width, height);
}

// return the optimum width of the table
static int largeur_optimal(QTableWidget& tableau, const short int nombre_lignes_maximum)
{
    int width = tableau.verticalHeader()->width();
    if(tableau.rowCount() <= nombre_lignes_maximum)
    {
        for(int i = 0; i < tableau.columnCount(); ++i)
        {
            width += tableau.columnWidth(i);
        }
    }
    else {
        for(int i = 0; i < nombre_lignes_maximum; ++i)
        {
            width += tableau.columnWidth(i);
        }
        width += tableau.verticalHeader()->sizeHint().width() + 3;
    }
    return width;
}

// return the optimum height of the table
static int hauteur_optimale(QTableWidget& tableau, const short int nombre_lignes_maximum)
{
    int height = tableau.horizontalHeader()->height();
    if(tableau.rowCount() <= nombre_lignes_maximum)
    {
        for(int i = 0; i < tableau.rowCount(); ++i)
        {
            height += tableau.rowHeight(i);
        }
        height += tableau.rowCount() - 1;
    }
    else {
        for(int i = 0; i < nombre_lignes_maximum; ++i)
        {
            height += tableau.rowHeight(i);
        }
        height += nombre_lignes_maximum - 1;
    }
    return height;
}

И мне хотелось бы что-то подобное:

enter image description here

...