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());
}
```*