C++ 如何确定QHBoxLayout项目的大小并向QHBoxLayout中的每个项目添加下拉列表
我有自定义窗口类C++ 如何确定QHBoxLayout项目的大小并向QHBoxLayout中的每个项目添加下拉列表,c++,qt,qlayout,C++,Qt,Qlayout,我有自定义窗口类 #define NAME_WIDTH 150 #define NAME_HEIGHT 20 ObjectWindow::ObjectWindow(QWidget * parent) { } void ObjectWindow::SetKey(KeyObject * keyObj) { QGridLayout * layout = new QGridLayout(this); nameField = new QTextEdit(this); nam
#define NAME_WIDTH 150
#define NAME_HEIGHT 20
ObjectWindow::ObjectWindow(QWidget * parent)
{
}
void ObjectWindow::SetKey(KeyObject * keyObj)
{
QGridLayout * layout = new QGridLayout(this);
nameField = new QTextEdit(this);
nameField->setText(keyObj->name);
nameField->setGeometry(nameField->geometry().x(), nameField->geometry().y(),
NAME_WIDTH, NAME_HEIGHT);
layout->addWidget(nameField);
QHBoxLayout * picsLayout = new QHBoxLayout(this);
for(std::vector<ImageInstance*>::iterator imgObj = keyObj->images.begin(); imgObj != keyObj->images.end(); imgObj++)
{
QComboBox * folderList = new QComboBox;
picsLayout->addWidget(folderList);
QImage image((*imgObj)->imgPath);
QLabel * picLabel = new QLabel;
picLabel->setPixmap(QPixmap::fromImage(image).scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));
picsLayout->addWidget(picLabel);
}
layout->addLayout(picsLayout, 2, 0);
QPushButton * saveBtn = new QPushButton(this);
saveBtn->setText("Save");
connect(saveBtn, SIGNAL(released()),this, SLOT(Save()));
layout->addWidget(saveBtn);
setLayout(layout);
}
#定义名称\u宽度150
#定义名称\u高度20
ObjectWindow::ObjectWindow(QWidget*父对象)
{
}
void ObjectWindow::SetKey(KeyObject*keyObj)
{
QGridLayout*layout=新的QGridLayout(本);
nameField=新的QTextEdit(此);
nameField->setText(keyObj->name);
名称字段->设置几何体(名称字段->几何体().x(),名称字段->几何体().y(),
名称(宽度、名称(高度));
布局->添加小部件(名称字段);
QHBoxLayout*picsLayout=新的QHBoxLayout(本);
对于(std::vector::iterator imgObj=keyObj->images.begin();imgObj!=keyObj->images.end();imgObj++)
{
QCOMBOX*folderList=新的QCOMBOX;
picsLayout->addWidget(文件夹列表);
QImage图像((*imgObj)->imgPath);
QLabel*picLabel=新的QLabel;
picLabel->setPixmap(QPixmap::fromImage(image).scaled(200200,Qt::KeepAspectRatio,Qt::SmoothTransformation));
picsLayout->addWidget(picLabel);
}
布局->添加布局(picsLayout,2,0);
QPushButton*saveBtn=新的QPushButton(此);
saveBtn->setText(“保存”);
连接(saveBtn,信号(released()),此,插槽(Save());
布局->添加小部件(saveBtn);
设置布局(布局);
}
我需要的是
- 设置名称的小文本字段,我不明白为什么SetGeometry不起作用
- 每个图像上方的下拉列表。我可以为每一组图像和列表创建QHVertical布局,但也许有更简单的方法可以做到这一点
如果您只想让用户设置名称,a可能就足够了 然后,使用QGridLayout的主要优点是不需要创建其他布局。它就像一个网格,您可以在其中放置小部件,有点像Excel(和其他电子表格程序) 哦,我看到你没有在构造器中构造小部件(看起来是空的),这是人们通常做的事情,因为构造UI可能很昂贵,你只想在相关的时候更新它,而不是重建整个UI来更新一个字段。但是如果没有更多的代码,我无法判断何时调用此函数 您可以尝试以下方法:
QGridLayout * layout = new QGridLayout(this);
nameField = new QLineEdit(this);
nameField->setText(keyObj->name);
layout->addWidget(nameField, 0, 0, -1, 1); // expand to the right edge
int currentColumn = 0;
for(std::vector<ImageInstance*>::iterator imgObj = keyObj->images.begin(); imgObj != keyObj->images.end(); imgObj++)
{
QComboBox * folderList = new QComboBox;
layout->addWidget(folderList, 1, currentColumn);
QPixmap pixmap((*imgObj)->imgPath);
pixmap = pixmap.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QLabel * picLabel = new QLabel(this);
picLabel->setPixmap(pixmap);
layout->addWidget(picLabel, 2, currentColumn);
++currentColumn;
}
QPushButton * saveBtn = new QPushButton("Save", this);
connect(saveBtn, SIGNAL(released()),this, SLOT(Save()));
layout->addWidget(saveBtn, 3, 0, -1, 1);
setLayout(layout);
QGridLayout*layout=新的QGridLayout(本);
nameField=新的QLineEdit(此);
nameField->setText(keyObj->name);
布局->添加小部件(名称字段,0,0,-1,1);//展开到右边缘
int currentColumn=0;
对于(std::vector::iterator imgObj=keyObj->images.begin();imgObj!=keyObj->images.end();imgObj++)
{
QCOMBOX*folderList=新的QCOMBOX;
布局->添加小部件(folderList,1,currentColumn);
QPixmap pixmap((*imgObj)->imgPath);
pixmap=pixmap.scaled(200200,Qt::KeepAspectRatio,Qt::SmoothTransformation);
QLabel*picLabel=新的QLabel(此);
picLabel->setPixmap(pixmap);
布局->添加小部件(picLabel,2,currentColumn);
++当前列;
}
QPushButton*saveBtn=新的QPushButton(“保存”,此项);
连接(saveBtn,信号(released()),此,插槽(Save());
布局->添加小部件(saveBtn,3,0,-1,1);
设置布局(布局);
但是像这样水平添加这些小部件似乎不是一个好主意。如果此向量中有100项,会发生什么?您应该研究如何使用QScrollArea或修改用户界面,以便为您的客户提供查看和编辑这些内容的最佳方式(但如果没有更多的上下文,似乎很难给出更多建议)。谢谢,这很有效!嗯,我知道将不会有超过6个图像,所以没有nedd滚动区域