Qt5学习笔记(6)——下拉列表框QComboBox类

最近需要做一个地址选择下拉列表,如下图:

Qt之QComboBox(基本应用、代理设置)

 

二、设置代理

    好了,此代理非彼代理也,综上所说的代理为QComboBox的选项,这里要说明的是QComboBox的代理组件!

先看此图:

Qt之QComboBox(基本应用、代理设置)

    可以看出下拉选项中不仅包含有文本信息,而且含包含有相应的组件!其实这是模拟的一个用户选择输入框,用户不仅可以输入帐号,而且可以选择想要登录的帐号,并且可进行帐号的删除!

 

    代理选项包含一个用户帐号文本和一个删除按钮,mouseReleaseEvent函数主要获取此代理的文本,用于显示在QComboBox中,删除按钮执行的是获取代理的文本,根据不同的文本删除对应的帐号信息!

(1)设定代理组成

AccountItem::AccountItem(QWidget *parent)
: QWidget(parent)
{
 mouse_press = false;
 account_number = new QLabel();
 delede_button = new QPushButton();

 QPixmap pixmap(“:/loginDialog/delete”);
 delede_button->setIcon(pixmap);
 delede_button->setIconSize(pixmap.size());
 delede_button->setStyleSheet(“background:transparent;”);
 connect(delede_button, SIGNAL(clicked()), this, SLOT(removeAccount()));

 QHBoxLayout *main_layout = new QHBoxLayout();
 main_layout->addWidget(account_number);
 main_layout->addStretch();
 main_layout->addWidget(delede_button);
 main_layout->setContentsMargins(5, 5, 5, 5);
 main_layout->setSpacing(5);
 this->setLayout(main_layout);
}

AccountItem::~AccountItem()
{

}

void AccountItem::setAccountNumber(QString account_text)
{
 account_number->setText(account_text);
}

QString AccountItem::getAccountNumber()
{
 QString account_number_text = account_number->text();

 return account_number_text;
}

void AccountItem::removeAccount()
{
 QString account_number_text = account_number->text();
 emit removeAccount(account_number_text);
}

void AccountItem::mousePressEvent(QMouseEvent *event)
{

 if(event->button() == Qt::LeftButton)
 {
  mouse_press = true;
 }
}

void AccountItem::mouseReleaseEvent(QMouseEvent *event)
{
 if(mouse_press)
 {
  emit showAccount(account_number->text());
  mouse_press = false;
 }
}

 

(2)添加代理至QComboBox:

 account_combo_box = new QComboBox();
 list_widget = new QListWidget();
 account_combo_box->setModel(list_widget->model());
 account_combo_box->setView(list_widget);
 account_combo_box->setEditable(true); //设置QComboBox可编辑
 for(int i=0; i<3; i++)
 {
  AccountItem *account_item = new AccountItem();
  account_item->setAccountNumber(QString(“safe_”) + QString::number(i, 10) + QString(“@sina.com”));
  connect(account_item, SIGNAL(showAccount(QString)), this, SLOT(showAccount(QString)));
  connect(account_item, SIGNAL(removeAccount(QString)), this, SLOT(removeAccount(QString)));
  QListWidgetItem *list_item = new QListWidgetItem(list_widget);
  list_widget->setItemWidget(list_item, account_item);
 }

 

(3)实现代理选项进行的操作

//将选项文本显示在QComboBox当中

void LoginDialog::showAccount(QString account)
{
 account_combo_box->setEditText(account);
 account_combo_box->hidePopup();
}

 

//删除帐号时,弹出提示框,与用户进行交互,告知是否确定要删除此帐号的所有信息!

来源:陈raiven

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2015年1月2日
下一篇 2015年1月2日

相关推荐