C++ QT-从.ui文件创建布局

C++ QT-从.ui文件创建布局,c++,qt,user-interface,C++,Qt,User Interface,我有如下代码。我在数据库中有一些数据,我需要创建视图结构来显示它。我在Qt编程方面没有太多经验,我用PHP、HTML和CSS做了很多东西。我需要做一些类似于HTML的事情——当你有一个没有额外样式的框(例如div)并且你把一些数据放在里面时,这个div标签将显示里面的所有数据。但通过下面的代码,我只获得了从文件加载的小部件的一部分数据。主窗口中GridLayout的行为类似于div style=“最大宽度:200px;最大高度:200px;溢出:隐藏”。而且子文件中的布局元素也具有相同的行为 #

我有如下代码。我在数据库中有一些数据,我需要创建视图结构来显示它。我在Qt编程方面没有太多经验,我用PHP、HTML和CSS做了很多东西。我需要做一些类似于HTML的事情——当你有一个没有额外样式的框(例如div)并且你把一些数据放在里面时,这个div标签将显示里面的所有数据。但通过下面的代码,我只获得了从文件加载的小部件的一部分数据。主窗口中GridLayout的行为类似于div style=“最大宽度:200px;最大高度:200px;溢出:隐藏”。而且子文件中的布局元素也具有相同的行为

#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();
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括“databasemanager.h”
#包括“ycexception.h”
#包括
#包括
#包括
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
createStructureFromDb();
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void MainWindow::createStructureFromDb(){
试一试{
dbManager=newdatabasemanager();
}
捕获(异常e){
//TODO:显示带有消息的对话框
qDebug()getGroups();
while(groupsQuery.next()){
组;
groupsQuery.value(0.toInt());
setName(groupsQuery.value(1.toString());
装载机;
QFile文件(“:/forms/group.ui”);
打开(QFile::ReadOnly);
QWidget*formWidget=loader.load(&file);
file.close();
(formWidget->findChild(“groupName”)->setText(group.getName());
ui->gridLayout->addWidget(formWidget);
QVBoxLayout*groupItems=formWidget->findChild(“groupItems”);
QSqlQuery cardsQuery=dbManager->getGroupCards(group.getIdGroup());
while(cardsQuery.next()){
卡片;
card.setIdCard(cardsQuery.value(0.toInt());
setContent(cardsQuery.value(1.toString());
card.setDueDate(cardsQuery.value(2.toString());
card.setIdGroup(cardsQuery.value(3.toInt());
组。添加卡(card);
QFile文件(“:/forms/card.ui”);
QWidget*cardWidget=loader.load(&file);
file.close();
(cardWidget->findChild(“contentLabel”)->setText(card.getContent());
(cardWidget->findChild(“dueDateLabel”)->setText(card.getDueDate());
groupItems->addWidget(cardWidget);
}
插入(group.getIdGroup(),group);
}
ui->label->setText(“真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本真长文本”);
此->布局()->激活();
}

溢出:对于像我这样对网络技术知之甚少的人来说,隐藏的几乎是明确的(但我可以用谷歌搜索)。但是,说你想要滚动条更具表现力……我过去常常理解你想要什么

因此,对于您想要的:
GridLayout
与html的div不同。如果您想要滚动小部件的内容,您必须将小部件放入,并将滚动区域放在第一个包含小部件的GridLayout的单元格中

更具体地说:

  • 溢出:可见
  • 溢出:隐藏
    :默认行为
  • 溢出:滚动
    :使用QScrollArea
  • 溢出:自动
    :使用QScrollArea
  • overflow:inherit
    :这是可能的,但要做到这一点,您需要编写大量的代码。而且,在设计良好的界面中也是无用的

那么,你的问题是什么?咒骂不会让你得到很多答案你的ui文件是什么样子的?谢谢你的回答。不幸的是,我不能用我的ui发布回复,因为我的声誉很低,所以也许明天我会把我的ui文件放在这里。我不太准确-我需要显示QWidget的内容,由card.ui和group.ui文件制作。我需要显示它的全部内容。现在我不知道它的大小应该是多少,因为它的内容将从数据库加载。所以可以设置QWidget的大小(以及它的元素-只有QLabel)来显示其中的全部文本吗?对于小部件,可以指定最小大小、最大大小、提示大小(首选大小),以及大小如何变化。使用此选项,您可以调整任何小部件的大小。对于文本的大小,您应该设置最大宽度和无限高度(更易于阅读)。感谢您的回复。当我加载小部件时,我会将其添加到容器(QVBoxLayout)。我也可以为它指定上述参数吗?你的意思是,指定布局的大小?否:你必须指定包含布局的小部件的大小(但结果将是相同的)。