How to set QGridLayout column width for QLabel Widgets?



  • I have been battling with learning how to properly implement the QGridLayout into my project for a week or so now, and feel I am almost there. However, currently my setup looks like "Main Window Grid Layout -> QScrollArea -> QWidget (Container) -> QGridLayout -> populated by QLabels". When I manually set the width of all of my labels to anything larger than their default size, they don't expand the columns and just start overlapping each other. I want to show 3 columns and 5 rows, but this still seems to be just squeezing them all in.

    @void MainWindow::setupEPGTable()
    {
    QScrollArea *scrollArea = new QScrollArea();
    QWidget *contentsWidget = new QWidget(scrollArea);
    gridEPG = new QGridLayout(contentsWidget);

    //scrollArea->setWidgetResizable(false);
    scrollArea->setWidget(contentsWidget);
    contentsWidget->setLayout(gridEPG);
    contentsWidget->setMinimumSize(scrollArea->width(), scrollArea->height());
    gridEPG->setHorizontalSpacing(5);
    ui->gridLayout->addWidget(scrollArea, 1, 0, 2, 3);
    
    // Top Left Box for displaying Date/Time
    QLabel *labelDate = new QLabel("WED");
    labelDate->setFixedWidth(200);
    labelDate->setStyleSheet("background-color: #ffffff");
    labelDate->setAlignment(Qt::AlignCenter);
    gridEPG->addWidget(labelDate, 0, 0);
    
    // Left Side Menu (column 0) for displaying channel Names/Logos
    for (int i=0; i < 5; i++)
    {
        QLabel *labelChannel = new QLabel("001");
        labelChannel->setFixedWidth(200);
        labelChannel->setStyleSheet("background-color: green;");
        labelChannel->setAlignment(Qt::AlignCenter);
        gridEPG->addWidget(labelChannel, i + 1, 0);
    }
    
    QTime time = QTime::currentTime();
    getClosestHalfHour(time);
    
    // Top row (row 0) for displaying the Time Block
    for (int i = 0; i < 3; i++)
    {
        int row = 0;
        int col = i+1;
    
        // Sets the Time Label
        QLabel *labelTime = new QLabel(time.toString("hh:mm"));
        labelTime->setFixedWidth(200);
        labelTime->setStyleSheet("background-color: red; border: 1px solid yellow;");
        labelTime->setAlignment(Qt::AlignCenter);
    
        // Adjusts the time by 30 minutes for the next iteration
        if (time.minute() == 00)
        {
            time.setHMS(time.hour(), 30, time.second());
        }
        else
        {
            time.setHMS(time.hour() + 1, 00, time.second());
        }
    
        gridEPG->addWidget(labelTime, row, col);
    }
    
    // The episode contents to be displayed according to the relevant channel (row) and time slot (column)
    for (int x = 0; x < 5; x++)
    {
        for (int i = 0; i < 3; i++)
        {
            int row = x+1;
            int col = i+1;
    
            QLabel *labelEPG = new QLabel("TEST EPG");
            labelEPG->setFixedWidth(200);
            labelEPG->setStyleSheet("background-color: blue; border: 1px solid orange;");
            labelEPG->setAlignment(Qt::AlignCenter);
            gridEPG->addWidget(labelEPG, row, col);
        }
    }
    

    }@

    !http://i47.tinypic.com/34dk1nt.png(Layout Screenshot)!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.