#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QDebug>
#include <QDialog>
#include <QItemSelectionModel>
#include <QModelIndex>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onButtonClicked()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(onButton2Clicked()));
connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(onButton3Clicked()));
connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(onButton4Clicked()));
QStringList list;
list << "1" << "2" << "3" << "4" << "5";
ui->comboBox->addItems(list);
}
Widget::~Widget()
{
delete ui;
}
void Widget::onButtonClicked()
{
//show popup
ui->comboBox->showPopup();
}
void Widget::onButton2Clicked()
{
//move down
m_index++;
m_index = m_index %4;
ui->comboBox->setCurrentIndex(m_index);
QModelIndex itemIndex = ui->comboBox->view()->model()->index(m_index,0);
ui->comboBox->view()->selectionModel()
->setCurrentIndex(itemIndex,QItemSelectionModel::SelectCurrent);
}
void Widget::onButton3Clicked()
{
//move up
m_index--;
m_index = m_index %4;
ui->comboBox->setCurrentIndex(m_index);
QModelIndex itemIndex = ui->comboBox->view()->model()->index(m_index,0);
ui->comboBox->view()->selectionModel()
->setCurrentIndex(itemIndex,QItemSelectionModel::SelectCurrent);
}
void Widget::onButton4Clicked()
{
//chose one
QModelIndex itemIndex = ui->comboBox->view()->model()->index(m_index,0);
ui->comboBox->view()->selectionModel()->setCurrentIndex(itemIndex,QItemSelectionModel::ToggleCurrent);
}
4个按钮~~第一个show 第2个光标下移。 第3个 光标上移 。 第4个选中。
当然。combox..失去焦点的时候默认的行为是hide住弹出的listview..