{"id":215289,"date":"2014-03-11T02:11:03","date_gmt":"2014-03-10T22:11:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=215289"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=215289","title":{"rendered":"<span class=\"post_title\">\u041c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 QComboBox<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/9a6\/ae0\/28e\/9a6ae028e8c259b38c7229130dcb7271.png\"\/><br \/>  <i>\u041a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f<br \/>  (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438\u043c\u0435\u044e\u0449\u0430\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043a \u043f\u043e\u0441\u0442\u0443)<\/i><\/p>\n<p>  \u0418\u043d\u043e\u0433\u0434\u0430, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0431\u044b\u0432\u0430\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0432\u0438\u0434\u0436\u0435\u0442\u0435 QComboBox. \u0412 \u044d\u0442\u043e\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u044d\u0442\u043e c\u0434\u0435\u043b\u0430\u0442\u044c.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u043c\u043e\u0434\u0435\u043b\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u0432 QComboBox, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u0444\u043b\u0430\u0436\u043e\u043a <i>Qt::ItemIsUserCheckable<\/i>, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0441\u0434\u0435\u043b\u0430\u0432 \u0438\u0445 \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u043c\u044b\u043c\u0438. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u043e \u0432\u044b\u0432\u043e\u0434\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430 \u0432\u0438\u0434\u0436\u0435\u0442\u0435.<\/p>\n<p>  \u041e\u0431\u044a\u044f\u0432\u0438\u043c \u043a\u043b\u0430\u0441\u0441 MultiListWidget (\u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b checkedItems \u0434\u0430\u044e\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u043f\u0438\u0441\u043a\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0438\u043b\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043c\u0435\u0442\u0438\u043b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0430 \u043c\u0435\u0442\u043e\u0434 collectCheckedItems \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 \u0432 mCheckedItems):  <\/p>\n<pre><code class=\"cpp\">class MultiListWidget \t: public QComboBox { \tQ_OBJECT  \tQ_PROPERTY(QStringList checkedItems READ checkedItems WRITE setCheckedItems)  public: \tMultiListWidget(); \tvirtual ~MultiListWidget();  \tQStringList checkedItems() const; \tvoid setCheckedItems(const QStringList &items);  private: \tQStringList mCheckedItems;  \tvoid collectCheckedItems();  }; <\/code><\/pre>\n<p>  \u0412 \u043c\u043e\u0434\u0435\u043b\u0438 QComboBox \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0445 \u043d\u0430\u043c \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432:  <\/p>\n<ul>\n<li>rowsInserted(const QModelIndex &#038;parent, int start, int end) \u2014 \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c (\u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 addItem, insertItem \u0438 \u0442.\u0434.)<\/li>\n<li>rowsRemoved(const QModelIndex &#038;parent, int start, int end) \u2014 \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0438 (\u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 removeItem)<\/li>\n<\/ul>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f itemChanged(QStandardItem *item), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0438 \u0441\u043d\u044f\u0442\u0438\u0438 \u0444\u043b\u0430\u0436\u043a\u0430 (\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0438\u043b\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e).<\/p>\n<p>  \u041e\u0431\u044a\u044f\u0432\u0438\u043c \u0441\u043b\u043e\u0442\u044b \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432:  <\/p>\n<pre><code class=\"cpp\">private slots: \tvoid slotModelRowsInserted(const QModelIndex &parent, int start, int end); \tvoid slotModelRowsRemoved(const QModelIndex &parent, int start, int end); \tvoid slotModelItemChanged(QStandardItem *item); <\/code><\/pre>\n<p>  \u0418 \u0441\u0432\u044f\u0436\u0435\u043c \u0441\u0438\u0433\u043d\u0430\u043b\u044b \u0441\u043e \u0441\u043b\u043e\u0442\u0430\u043c\u0438 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 (\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e model() \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 QAbstractItemModel, \u0430 \u0441\u0438\u0433\u043d\u0430\u043b itemChanged \u0438\u0441\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 QStandardItemModel, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0443\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435):  <\/p>\n<pre><code class=\"cpp\">MultiListWidget::MultiListWidget() { \tconnect(model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(slotModelRowsInserted(QModelIndex,int,int))); \tconnect(model(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(slotModelRowsRemoved(QModelIndex,int,int)));  \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*))); }  MultiListWidget::~MultiListWidget() { } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b checkedItems() \u0438 setCheckedItems(const QStringList &#038;items):  <\/p>\n<pre><code class=\"cpp\">QStringList MultiListWidget::checkedItems() const { \treturn mCheckedItems; }  void MultiListWidget::setCheckedItems(const QStringList &items) { \t\/\/ \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \t\/\/ \u043e\u0442\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u0441\u0438\u0433\u043d\u0430\u043b\u0430, \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u0444\u043b\u0430\u0436\u043a\u043e\u0432 \tdisconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*)));  \tfor (int i = 0; i &lt; items.count(); ++i) \t{ \t\t\/\/ \u0438\u0449\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \t\tint index = findText(items.at(i));  \t\tif (index != -1) \t\t{ \t\t\t\/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0444\u043b\u0430\u0436\u043e\u043a \t\t\tstandartModel-&gt;item(index)-&gt;setData(Qt::Checked, Qt::CheckStateRole); \t\t} \t}  \t\/\/ \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u0441\u044f \u043a \u0441\u0438\u0433\u043d\u0430\u043b\u0443 \tdisconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*)));  \t\/\/ \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \tcollectCheckedItems(); } <\/code><\/pre>\n<p>  \u0412\u043d\u0443\u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 collectCheckedItems() \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u043f\u0440\u043e\u0431\u0435\u0433\u0430\u0435\u043c\u0441\u044f \u043f\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u043c\u043e\u0434\u0435\u043b\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d \u043e\u0442\u043c\u0435\u0447\u0435\u043d, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a:  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::collectCheckedItems() { \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tmCheckedItems.clear();  \tfor (int i = 0; i &lt; count(); ++i) \t{ \t\tQStandardItem *currentItem = standartModel-&gt;item(i);  \t\tQt::CheckState checkState = static_cast&lt;Qt::CheckState&gt;(currentItem-&gt;data(Qt::CheckStateRole).toInt());  \t\tif (checkState == Qt::Checked) \t\t{ \t\t\tmCheckedItems.push_back(currentItem-&gt;text()); \t\t} \t} } <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u043c\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0438 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0441\u043e \u0441\u043d\u044f\u0442\u044b\u043c \u0444\u043b\u0430\u0436\u043a\u043e\u043c:  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::slotModelRowsInserted(const QModelIndex &parent, int start, int end) { \t\/\/ \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0440\u0443\u0433\u0430\u043b\u0441\u044f \t(void)parent;  \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tdisconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*)));  \tfor (int i = start; i &lt;= end; ++i) \t{ \t\tstandartModel-&gt;item(i)-&gt;setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); \t\tstandartModel-&gt;item(i)-&gt;setData(Qt::Unchecked, Qt::CheckStateRole); \t}  \tconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*))); } <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0445 \u0438\u0437 mCheckedItems. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f collectCheckedItems():  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::slotModelRowsRemoved(const QModelIndex &parent, int start, int end) { \t(void)parent; \t(void)start; \t(void)end;  \tcollectCheckedItems(); } <\/code><\/pre>\n<p>  \u0412 \u0441\u043b\u043e\u0442\u0435 slotModelItemChanged(QStandardItem *item) \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b:  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::slotModelItemChanged(QStandardItem *item) { \t(void)item;  \tcollectCheckedItems(); } <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, multilist.h \u0438 multilist.cpp, \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c MultiListWidget \u0432 \u0434\u0435\u043b\u0435 (\u0444\u0430\u0439\u043b main.cpp):  <\/p>\n<pre><code class=\"cpp\">#include &quot;multilist.h&quot;  int main(int argc, char *argv[]) { \tQApplication app(argc, argv);  \tMultiListWidget *multiList = new MultiListWidget();  \tmultiList-&gt;addItems(QStringList() &lt;&lt; &quot;One&quot; &lt;&lt; &quot;Two&quot; &lt;&lt; &quot;Three&quot; &lt;&lt; &quot;Four&quot;); \tmultiList-&gt;setCheckedItems(QStringList() &lt;&lt; &quot;One&quot; &lt;&lt; &quot;Three&quot;);  \tQHBoxLayout *layout = new QHBoxLayout();  \tlayout-&gt;addWidget(new QLabel(&quot;Select items:&quot;)); \tlayout-&gt;addWidget(multiList, 1);  \tQWidget widget;  \twidget.setWindowTitle(&quot;MultiList example&quot;); \twidget.setLayout(layout);  \twidget.show();  \treturn app.exec(); } <\/code><\/pre>\n<p>  \u041d\u0435\u043f\u043b\u043e\u0445\u043e, \u043d\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0435\u0449\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 \u0432\u0438\u0434\u0436\u0435\u0442\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u043c (\u0432 \u0437\u0430\u043a\u0440\u044b\u0442\u043e\u0439 \u0441\u0435\u043a\u0446\u0438\u0438) \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430, \u0434\u0435\u043b\u044c\u0442\u0443 \u0434\u043b\u044f \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430 (\u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0438\u0436\u0435), \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0442\u0435\u043a\u0441\u0442, \u0438 \u043c\u0435\u0442\u043e\u0434, \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0442\u0435\u043a\u0441\u0442 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432:  <\/p>\n<pre><code class=\"cpp\">QString mDisplayText; const QRect mDisplayRectDelta;  void updateDisplayText(); <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e mDisplayRectDelta:  <\/p>\n<pre><code class=\"cpp\">MultiListWidget::MultiListWidget() \t: mDisplayRectDelta(4, 1, -25, 0) { \t... } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 updateDisplayText():  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::updateDisplayText() { \t\/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430, mDisplayRectDelta \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442 \u0442\u0435\u043a\u0441\u0442 &quot;\u0432\u043e\u0432\u043d\u0443\u0442\u0440\u044c&quot; \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \t\/\/ \u0441 \u0443\u0447\u0451\u0442\u043e\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043d\u043e\u043f\u043a\u0430, \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0430\u044f \u0441\u043f\u0438\u0441\u043e\u043a \tQRect textRect = rect().adjusted(mDisplayRectDelta.left(), mDisplayRectDelta.top(), \t\t\t\t\t\t\t\t\t mDisplayRectDelta.right(), mDisplayRectDelta.bottom());  \tQFontMetrics fontMetrics(font());  \t\/\/ \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u044f\u0442\u044b\u043c\u0438 \tmDisplayText = mCheckedItems.join(&quot;, &quot;);  \t\/\/ \u0435\u0441\u043b\u0438 \u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043b\u0430\u0437\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \tif (fontMetrics.size(Qt::TextSingleLine, mDisplayText).width() &gt; textRect.width()) \t{ \t\t\/\/ \u043e\u0431\u0440\u0435\u0437\u0430\u0435\u043c \u0435\u0433\u043e \u043f\u043e\u0441\u0438\u043c\u0432\u043e\u043b\u044c\u043d\u043e, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \t\twhile (mDisplayText != &quot;&quot; && fontMetrics.size(Qt::TextSingleLine, mDisplayText + &quot;...&quot;).width() &gt; textRect.width()) \t\t{ \t\t\tmDisplayText.remove(mDisplayText.length() - 1, 1); \t\t}  \t\t\/\/ \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0442\u0440\u043e\u0435\u0442\u043e\u0447\u0438\u0435\u043c \t\tmDisplayText += &quot;...&quot;; \t} } <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 paintEvent(QPaintEvent *event). \u0422\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 resizeEvent(QResizeEvent *event), \u0442\u0430\u043a \u043a\u0430\u043a \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0442\u0435\u043a\u0441\u0442\u0430 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u0441\u044f. \u0412\u043e\u0442 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432:  <\/p>\n<pre><code class=\"cpp\">protected: \tvirtual void paintEvent(QPaintEvent *event); \tvirtual void resizeEvent(QResizeEvent *event); <\/code><\/pre>\n<p>  \u0418 \u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::paintEvent(QPaintEvent *event) { \t(void)event;  \tQStylePainter painter(this);  \tpainter.setPen(palette().color(QPalette::Text));  \tQStyleOptionComboBox option;  \tinitStyleOption(&option);  \t\/\/ \u0440\u0438\u0441\u0443\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \tpainter.drawComplexControl(QStyle::CC_ComboBox, option);  \t\/\/ \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \tQRect textRect = rect().adjusted(mDisplayRectDelta.left(), mDisplayRectDelta.top(), \t\t\t\t\t\t\t\t\t mDisplayRectDelta.right(), mDisplayRectDelta.bottom());  \t\/\/ \u0440\u0438\u0441\u0443\u0435\u043c \u0442\u0435\u043a\u0441\u0442 \tpainter.drawText(textRect, Qt::AlignVCenter, mDisplayText); }  void MultiListWidget::resizeEvent(QResizeEvent *event) { \t(void)event;  \tupdateDisplayText(); } <\/code><\/pre>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043e\u043d\u0435\u0446 collectCheckedItems() \u0432\u044b\u0437\u043e\u0432 updateDisplayText() \u0438 \u043f\u0435\u0440\u0435\u0440\u0438\u0441\u0443\u0435\u043c \u0432\u0438\u0434\u0436\u0435\u0442:  <\/p>\n<pre><code class=\"cpp\">void MultiListWidget::setCheckedItems(const QStringList &items) { \t... \tupdateDisplayText(); \trepaint(); } <\/code><\/pre>\n<p>  \u0412 \u0441\u0442\u0438\u043b\u044f\u0445 GTK \u0438 Mac \u0435\u0441\u0442\u044c <a href=\"https:\/\/bugreports.qt-project.org\/browse\/QTBUG-3948\">\u0431\u0430\u0433<\/a>, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043d\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0444\u043b\u0430\u0436\u043a\u0438 \u0432 \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435. \u0414\u043b\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0430\u0433\u0430 \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f combobox-popup \u0432 styleSheet \u0432\u0438\u0434\u0436\u0435\u0442\u0430 (\u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440):  <\/p>\n<pre><code class=\"cpp\">setStyleSheet(&quot;QComboBox { combobox-popup: 1px }&quot;); <\/code><\/pre>\n<p>  \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f:<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/8ef\/071\/47e\/8ef07147ebd93f6b898d54cc37868099.png\"\/><\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/1b0\/13e\/52a\/1b013e52ad697d7a7969ac56b373ab14.png\"\/><\/p>\n<p>  \u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">multilist.h<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#ifndef MULTILIST_H #define MULTILIST_H  #include &lt;QtGui&gt;  class MultiListWidget \t: public QComboBox { \tQ_OBJECT  \tQ_PROPERTY(QStringList checkedItems READ checkedItems WRITE setCheckedItems)  public: \tMultiListWidget(); \tvirtual ~MultiListWidget();  \tQStringList checkedItems() const; \tvoid setCheckedItems(const QStringList &items);  protected: \tvirtual void paintEvent(QPaintEvent *event); \tvirtual void resizeEvent(QResizeEvent *event);  private: \tQStringList mCheckedItems;  \tvoid collectCheckedItems();  \tQString mDisplayText; \tconst QRect mDisplayRectDelta;  \tvoid updateDisplayText();  private slots: \tvoid slotModelRowsInserted(const QModelIndex &parent, int start, int end); \tvoid slotModelRowsRemoved(const QModelIndex &parent, int start, int end); \tvoid slotModelItemChanged(QStandardItem *item);  };  #endif \/\/ MULTILIST_H <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">multilist.cpp<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &quot;multilist.h&quot;  MultiListWidget::MultiListWidget() \t: mDisplayRectDelta(4, 1, -25, 0) { \tsetStyleSheet(&quot;QComboBox { combobox-popup: 1px }&quot;);  \tconnect(model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(slotModelRowsInserted(QModelIndex,int,int))); \tconnect(model(), SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(slotModelRowsRemoved(QModelIndex,int,int)));  \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*))); }  MultiListWidget::~MultiListWidget() { }  QStringList MultiListWidget::checkedItems() const { \treturn mCheckedItems; }  void MultiListWidget::setCheckedItems(const QStringList &items) { \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tdisconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*)));  \tfor (int i = 0; i &lt; items.count(); ++i) \t{ \t\tint index = findText(items.at(i));  \t\tif (index != -1) \t\t{ \t\t\tstandartModel-&gt;item(index)-&gt;setData(Qt::Checked, Qt::CheckStateRole); \t\t} \t}  \tconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*)));  \tcollectCheckedItems(); }  void MultiListWidget::paintEvent(QPaintEvent *event) { \t(void)event;  \tQStylePainter painter(this);  \tpainter.setPen(palette().color(QPalette::Text));  \tQStyleOptionComboBox option;  \tinitStyleOption(&option);  \tpainter.drawComplexControl(QStyle::CC_ComboBox, option);  \tQRect textRect = rect().adjusted(mDisplayRectDelta.left(), mDisplayRectDelta.top(), \t\t\t\t\t\t\t\t\t mDisplayRectDelta.right(), mDisplayRectDelta.bottom());  \tpainter.drawText(textRect, Qt::AlignVCenter, mDisplayText); }  void MultiListWidget::resizeEvent(QResizeEvent *event) { \t(void)event;  \tupdateDisplayText(); }  void MultiListWidget::collectCheckedItems() { \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tmCheckedItems.clear();  \tfor (int i = 0; i &lt; count(); ++i) \t{ \t\tQStandardItem *currentItem = standartModel-&gt;item(i);  \t\tQt::CheckState checkState = static_cast&lt;Qt::CheckState&gt;(currentItem-&gt;data(Qt::CheckStateRole).toInt());  \t\tif (checkState == Qt::Checked) \t\t{ \t\t\tmCheckedItems.push_back(currentItem-&gt;text()); \t\t} \t}  \tupdateDisplayText();  \trepaint(); }  void MultiListWidget::updateDisplayText() { \tQRect textRect = rect().adjusted(mDisplayRectDelta.left(), mDisplayRectDelta.top(), \t\t\t\t\t\t\t\t\t mDisplayRectDelta.right(), mDisplayRectDelta.bottom());  \tQFontMetrics fontMetrics(font());  \tmDisplayText = mCheckedItems.join(&quot;, &quot;);  \tif (fontMetrics.size(Qt::TextSingleLine, mDisplayText).width() &gt; textRect.width()) \t{ \t\twhile (mDisplayText != &quot;&quot; && fontMetrics.size(Qt::TextSingleLine, mDisplayText + &quot;...&quot;).width() &gt; textRect.width()) \t\t{ \t\t\tmDisplayText.remove(mDisplayText.length() - 1, 1); \t\t}  \t\tmDisplayText += &quot;...&quot;; \t} }  void MultiListWidget::slotModelRowsInserted(const QModelIndex &parent, int start, int end) { \t(void)parent;  \tQStandardItemModel *standartModel = qobject_cast&lt;QStandardItemModel*&gt;(model());  \tdisconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*)));  \tfor (int i = start; i &lt;= end; ++i) \t{ \t\tstandartModel-&gt;item(i)-&gt;setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); \t\tstandartModel-&gt;item(i)-&gt;setData(Qt::Unchecked, Qt::CheckStateRole); \t}  \tconnect(standartModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotModelItemChanged(QStandardItem*))); }  void MultiListWidget::slotModelRowsRemoved(const QModelIndex &parent, int start, int end) { \t(void)parent; \t(void)start; \t(void)end;  \tcollectCheckedItems(); }  void MultiListWidget::slotModelItemChanged(QStandardItem *item) { \t(void)item;  \tcollectCheckedItems(); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">main.cpp<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &quot;multilist.h&quot;  int main(int argc, char *argv[]) { \tQApplication app(argc, argv);  \tMultiListWidget *multiList = new MultiListWidget();  \tmultiList-&gt;addItems(QStringList() &lt;&lt; &quot;One&quot; &lt;&lt; &quot;Two&quot; &lt;&lt; &quot;Three&quot; &lt;&lt; &quot;Four&quot;); \tmultiList-&gt;setCheckedItems(QStringList() &lt;&lt; &quot;One&quot; &lt;&lt; &quot;Three&quot;);  \tQHBoxLayout *layout = new QHBoxLayout();  \tlayout-&gt;addWidget(new QLabel(&quot;Select items:&quot;)); \tlayout-&gt;addWidget(multiList, 1);  \tQWidget widget;  \twidget.setWindowTitle(&quot;MultiList example&quot;); \twidget.setLayout(layout);  \twidget.show();  \treturn app.exec(); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/215289\/\"> http:\/\/habrahabr.ru\/post\/215289\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/9a6\/ae0\/28e\/9a6ae028e8c259b38c7229130dcb7271.png\"\/><br \/>  <i>\u041a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f<br \/>  (\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438\u043c\u0435\u044e\u0449\u0430\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043a \u043f\u043e\u0441\u0442\u0443)<\/i><\/p>\n<p>  \u0418\u043d\u043e\u0433\u0434\u0430, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0431\u044b\u0432\u0430\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0432\u0438\u0434\u0436\u0435\u0442\u0435 QComboBox. \u0412 \u044d\u0442\u043e\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u044d\u0442\u043e c\u0434\u0435\u043b\u0430\u0442\u044c.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-215289","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/215289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215289"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/215289\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}