0

I have code like below. I have some data in database and I need to create view structure to display it. I don't have big experience in Qt programming, I've made more things in PHP, HTML and CSS. I need to do something like in HTML - when you have a box (for example div) without extra style and you put inside some data this div tag will display all data's inside. But with following code I've got only a part of datas from widgets loaded from file. Behavior of GridLayout in MainWindow is similar to div style="max-width: 200px; max-height: 200px; overflow:hidden". And also Layout elements from children's file has the same behavior...

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "databasemanager.h"
#include "ycexception.h"
#include <QDebug>
#include <QSqlQuery>

#include <QtUiTools>

#include <QLabel>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    createStructureFromDb();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createStructureFromDb() {
    try {
        dbManager = new DatabaseManager();
    }
    catch(YCException e) {
        //TODO: show dialog with message
        qDebug() << e.what();
        exit(-1);
    }

    QSqlQuery groupsQuery = dbManager->getGroups();
    while(groupsQuery.next()) {
        Group group;
        group.setIdGroup(groupsQuery.value(0).toInt());
        group.setName(groupsQuery.value(1).toString());

        QUiLoader loader;
        QFile file(":/forms/group.ui");
        file.open(QFile::ReadOnly);
        QWidget *formWidget = loader.load(&file);
        file.close();
        (formWidget->findChild<QLabel*>("groupName"))->setText(group.getName());
        ui->gridLayout->addWidget(formWidget);

        QVBoxLayout* groupItems = formWidget->findChild<QVBoxLayout*>("groupItems");

        QSqlQuery cardsQuery = dbManager->getGroupCards(group.getIdGroup());
        while(cardsQuery.next()) {
            Card card;
            card.setIdCard(cardsQuery.value(0).toInt());
            card.setContent(cardsQuery.value(1).toString());
            card.setDueDate(cardsQuery.value(2).toString());
            card.setIdGroup(cardsQuery.value(3).toInt());

            group.addCard(card);

            QFile file(":/forms/card.ui");
            QWidget *cardWidget = loader.load(&file);
            file.close();

            (cardWidget->findChild<QLabel*>("contentLabel"))->setText(card.getContent());
            (cardWidget->findChild<QLabel*>("dueDateLabel"))->setText(card.getDueDate());

            groupItems->addWidget(cardWidget);
        }
        groups.insert(group.getIdGroup(), group);
    }

    ui->label->setText("really long textreally long textreally long textreally long textreally long textreally long textreally long textreally long textreally long textreally long textreally long text");
    this->layout()->activate();
}
Synxis
  • 9,236
  • 2
  • 42
  • 64
Filip Golonka
  • 327
  • 1
  • 2
  • 9

1 Answers1

1

overflow:hidden is all but explicit for persons like me that have only a very little knowledge of web technologies (but I can google it). But, saying that you want scrollbars is a lot more expressive... I used this page to understand what you want.

So, for what you want: GridLayout is not like html's div. If you want the content of a widget to be scrolled, you have to put the widget in a QScrollArea, and put that scroll area in the cell of the GridLayout that first contained your widget.

More specifically:

  • overflow:visible : not possible
  • overflow:hidden : default behavior
  • overflow:scroll : use QScrollArea
  • overflow:auto : use QScrollArea
  • overflow:inherit : possible, but you need to write quite a bit of code to do this. Also, useless in a well-designed interface
Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64
  • thank you for the answer. Unfortunately I can't post reply with my ui, because I have low reputation, so maybe tomorrow I will put here my ui files. I'm not so precise - I need to show content of QWidget, maden by card.ui and group.ui files. I need to show full content of it. Now I don't know what it's size should be, because it's content will be loaded from database. So is it possible to set size of QWidget (and it's elements - there is only QLabel) to show whole text which is in it? – Filip Golonka Nov 29 '12 at 21:44
  • For a widget you can specify min size, max size, *hint* size (the preferred size), and how the size changes. With this, you can resize any widget. For the size of the text, you should set a maximum width, and unlimited height (easier to read). – Synxis Nov 29 '12 at 21:48
  • thank you for reply. When I load the widget, I add it to container (QVBoxLayout). Can I also specify above parameters for it? – Filip Golonka Nov 29 '12 at 22:24
  • You mean, specifying the size of a layout ? No: you have to specify the size of the widget containing the layout (but the result will be the same). – Synxis Nov 30 '12 at 18:30