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