Qt-QComboBox改变显示方式和位置

setView

设置combobox的显示窗口,并且设置tableview的选中项为当前显示项

ui->comboBox->setModel(model);
QTableView *tableView = new QTableView;
      tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
      tableView->setMinimumWidth(300);
      ui->comboBox->setView(tableView);
      connect(ui->comboBox->view(), &QAbstractItemView::pressed, [&](){
          oldText = ui->comboBox->model()->data(ui->comboBox->view()->currentIndex()).toString();
          qDebug() << ui->comboBox->model()->data(ui->comboBox->view()->currentIndex()).toString() << oldText;
      });
      connect(ui->comboBox,&QComboBox::currentTextChanged,[&]() {
          ui->comboBox->blockSignals(true);
          ui->comboBox->setCurrentText(oldText);
          ui->comboBox->blockSignals(false);
      });

showPopup

重写显示方式,显示的位置位于combobox上方

void MyComboBox::showPopup()
{
    QComboBox::showPopup();
    QWidget *popup = this->findChild<QFrame*>();
    popup->move(popup->x(),popup->y()-this->height()-popup->height());
}
```*

猜你喜欢

转载自blog.csdn.net/wayrboy/article/details/84828010