Search code examples
qtsearchadditionqtreewidget

Small panel at the bottom of a QTreeWidget


I'm using QT 5.4.2 and trying to create a small panel at the bottom of a subclassed QTreeWidget. Here is the code:

void HmiScenarioAutoscriptPanel::searchEmitter() {
    QWidget *child = new QWidget(ui->emitterTreeWidget);
    //QMainWindow* child = new QMainWindow;
    QLabel *labelSearch = new QLabel("Search");
    QLineEdit *lineSearch = new QLineEdit();

    lineSearch->setFixedSize(100, 20);

    QHBoxLayout* layout = new QHBoxLayout(ui->emitterTreeWidget);
    layout->setAlignment(Qt::AlignBottom);

    layout->addWidget(child);
   layout->addWidget(labelSearch);
   layout->addWidget(lineSearch);

}

The label and search field correctly appear at the bottom of the tree, however the fields overlap with the tree nodes (see image below).

Any idea why this behavior?

Ciao Alf

enter image description here


Solution

  • It is not recommended to set layout on the tree widget. It is like other controls like a button, label etc..

    I see that you are using designer. Add a blank widget (searchWidget) under the tree widget and then

    void HmiScenarioAutoscriptPanel::searchEmitter() {
        QWidget *child = new QWidget(ui->searchWidget);
        //QMainWindow* child = new QMainWindow;
        QLabel *labelSearch = new QLabel("Search", searchWidget);
        QLineEdit *lineSearch = new QLineEdit(searchWidget);
    
        lineSearch->setFixedSize(100, 20);
    
        QHBoxLayout* layout = new QHBoxLayout(ui->searchWidget);
        layout->setAlignment(Qt::AlignBottom);
    
        layout->addWidget(child);
       layout->addWidget(labelSearch);
       layout->addWidget(lineSearch);    
    }
    

    Just out of curiosity, why don't you add these using the designer as well?