How to fix QHBoxLayout items size and add drop-down list to every item in QHBoxLayout

StackOverflow https://stackoverflow.com/questions/22914357

  •  29-06-2023
  •  | 
  •  

質問

I have custom window class

#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);
}

What i need is

  • small text field to set the name, I don't unerstand why SetGeometry doesn't work

  • dropdown list above each image. I can create QHVertical layout for each set of image and list, but maybe there is more simple way to do it?

enter image description here

役に立ちましたか?

解決

If you just want the user to set the name, a QLineEdit is probably enough.

Then the main advantage of using a QGridLayout is that you don't need to create other layouts. It acts like a grid where you put your widgets, a bit like Excel (and other spreadsheet programs).

Oh and I see that you are not constructing the Widgets in the constructor (which seems to be empty), that's what people usually do because constructing the UI can be expensive and you just want to update it when relevant, not rebuilding the whole UI just to update a field. But without more code I cannot tell when this function is being called.

You can try something like this:

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);

But it doesn't seem to be a good idea to add those widgets horizontally like that. What would happen if there are 100 items in this vector? You should investigate in using something like a QScrollArea or modifying the UI to give your client the best way to view and edit those (but without more context it seems difficult to give your more advice).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top