Russian Qt Forum
Март 28, 2024, 12:23 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: ComboBox для работы с ключевыми полями  (Прочитано 2684 раз)
GraninDm
Гость
« : Февраль 12, 2016, 13:09 »

Добрый день!
Набросал класс для работы с моделью формируемой как ID, DisplayText
Класс используется примерно так
Код:
   comboBox1 = new BaseKeyComboBox();
    comboBox1->setModel(sourceModel); //модель примерно такая "SELECT RecordID, DisplayText FROM MyCatalog;"
    comboBox1->setModelColumn(1); // Колонка для отображения текста
    comboBox1->setKeyColumn(0); // Колонка для ключа ID ТОЛЬКО int!!!!!!!
    comboBox1->setEditable(false);

    // mapper - это QDataWidgetMapper
    // Подключается как обычно
    mapper->addMapping(comboBox1, mainModel->fieldIndex("referemceid"));

Сам класс
Код:
#ifndef BASEKEYCOMBOBOX_H
#define BASEKEYCOMBOBOX_H

#include <QWidget>
#include <QComboBox>

class BaseKeyComboBox : public QComboBox
{
    Q_OBJECT

    Q_PROPERTY(int currentKey READ currentKey WRITE setCurrentKey NOTIFY currentKeyChanged USER true)

public:
    explicit BaseKeyComboBox(QWidget *parent = 0);
    ~BaseKeyComboBox();

    int currentKey(){return m_currentKey;}
    void setKeyColumn(const int column);
public Q_SLOTS:
    void setCurrentKey(int key);
    void indexChanged(int index);
    void indexChanged(const QString &text);
    void textChanged(const QString &text);
Q_SIGNALS:
    void currentKeyChanged(const int key);

private:
    int m_currentKey;
    int m_keyColumn;
};

#endif // BASEKEYCOMBOBOX_H

Код:
#include "basekeycombobox.h"

#include <QComboBox>
//#include <QAbstractItemModel>


BaseKeyComboBox::BaseKeyComboBox(QWidget *parent)
    : QComboBox(parent),
    m_currentKey(0),
    m_keyColumn(0)
{
    connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(indexChanged(int)));
    connect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(indexChanged(QString)));
    connect(this, SIGNAL(currentTextChanged(QString)), this, SLOT(textChanged(QString)));
}

BaseKeyComboBox::~BaseKeyComboBox()
{

}

void BaseKeyComboBox::setKeyColumn(const int column)
{
    m_keyColumn = column;
}

void BaseKeyComboBox::setCurrentKey(int key)
{
    int row;
    for(row = 0; row < model()->rowCount(); row++){
        if (model()->data(model()->index(row,  m_keyColumn)).toInt() == key) {
            int key = model()->data(model()->index(row, m_keyColumn)).toInt();
            if(m_currentKey != key) {
                m_currentKey = key;
                setCurrentIndex(row);
                emit currentKeyChanged(key);
            }
            return;
        }
    }
}

void BaseKeyComboBox::indexChanged(int index)
{
    int key = model()->data(model()->index(index, m_keyColumn)).toInt();
    if(m_currentKey != key) {
        m_currentKey = key;
        emit currentKeyChanged(m_currentKey);
    }
}

void BaseKeyComboBox::indexChanged(const QString &text)
{
    int row;
    for(row = 0; row < model()->rowCount(); row++){
        if (model()->data(model()->index(row,  m_keyColumn)).toString() == text) {
            int key = model()->data(model()->index(row, m_keyColumn)).toInt();
            if(m_currentKey != key) {
                m_currentKey = key;
                emit currentKeyChanged(m_currentKey);
            }
            return;
        }
    }
}

void BaseKeyComboBox::textChanged(const QString &text)
{
    int row;
    for(row = 0; row < model()->rowCount(); row++){
        if (model()->data(model()->index(row,  modelColumn())).toString() == text) {
            int key = model()->data(model()->index(row, m_keyColumn)).toInt();
            if(m_currentKey != key) {
                m_currentKey = key;
                emit currentKeyChanged(m_currentKey);
            }
            return;
        }
    }
}
В решения не стал кидать, потому как класс писал на скорую руку.
Может быть кому пригодится.
« Последнее редактирование: Февраль 12, 2016, 13:39 от GraninDm » Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.044 секунд. Запросов: 20.