Qt5学习笔记(6)——下拉列表框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:

 

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

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

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

 

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

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

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

相关推荐