通过Qt中的QDataWidgetMapper将单选按钮连接到QSqlTableModel

时间:2015-06-24 08:45:37

标签: c++ qt user-interface orm

我想按照this forum thread将单选按钮映射到QDataWidgetMapper。我不确定如何挂钩提到的代表。有人能解释一下吗?

我已经有了包含单选按钮的.ui文件,在QLineEdits等中。这些文件已经通过QDataWidgetMapper挂钩到QSqlTableModel。我可以使用上面url中提到的解决方案将.ui字段中现有的单选按钮添加到按钮组,并将状态输入数据库吗?

我的单选按钮实际上是二进制选择,因此在db中存储布尔值就足够了。我不想使用复选框,因为在这种情况下,单选按钮使用户更容易理解选择。

到目前为止,我已将示例.h文件添加了包含

#include <QWidget>
#include <QButtonGroup>
#include <QVBoxLayout>
#include <QAbstractButton>

并创建.cpp文件。

#include "buttongroup.h"

ButtonGroup::ButtonGroup(QWidget *parent) :
    QWidget(parent)
{
    m_buttonGroup=new QButtonGroup(this);
    m_layout=new QVBoxLayout(this);
    connect(m_buttonGroup,SIGNAL(buttonClicked(int)),this,SIGNAL(buttonClicked(int)));
    setLayout(m_layout);

}

int ButtonGroup::getCheckedId()
{
    int id=m_buttonGroup->id(m_buttonGroup->checkedButton());
    return id;
}

void ButtonGroup::checkId(int id)
{
    m_buttonGroup->button(id)->setChecked(true);
}

void ButtonGroup::addButton(QAbstractButton *button, int id)
{
    m_buttonGroup->addButton(button,id);
    m_layout->addWidget(button);
    //connect(button,SIGNAL(clicked()),this,SIGNAL(buttonClicked()));
}

1 个答案:

答案 0 :(得分:2)

委托需要连接到映射器。

我创建了一个适用于http://scanrobot.fi/wp-content/uploads/2015/06/qradiobutton-qsqltablemodel.zip

的示例项目

同样在:https://github.com/savolai/qt_qradiobutton_qdatawidgetmapper_example

以下是我在实践中需要理解的内容:

QDataWidgetMapper* mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->toLast();

QRadioButton *b1=new QRadioButton("a",this);
QRadioButton *b2=new QRadioButton("b",this);

RadioButtonDelegate *delegate=new RadioButtonDelegate(this);
ButtonGroup *group=new ButtonGroup(this);
mapper->addMapping(group,model->fieldIndex("radio"));

// set the delegate as the item delegate of mapper
mapper->setItemDelegate(delegate);
// connect value change in group to delegate so it can send appropriate signals 
connect(group,SIGNAL(buttonClicked(int)),delegate,SLOT(commitMyData()));

group->addButton(b1,1);
group->addButton(b2,0);

buttongroup.cpp的来源:

#include "buttongroup.h"

ButtonGroup::ButtonGroup(QWidget *parent) :
    QWidget(parent)
{
    m_buttonGroup=new QButtonGroup(this);
    connect(m_buttonGroup,SIGNAL(buttonClicked(int)),this,SIGNAL(buttonClicked(int)));

}

int ButtonGroup::getCheckedId()
{
    int id=m_buttonGroup->id(m_buttonGroup->checkedButton());
    return id;
}

void ButtonGroup::checkId(int id)
{
    m_buttonGroup->button(id)->setChecked(true);
}

void ButtonGroup::addButton(QAbstractButton *button, int id)
{
    m_buttonGroup->addButton(button,id);
}

buttongroup.h(与原始网址相比,此处没有什么新内容):

#ifndef BUTTONGROUP_H
#define BUTTONGROUP_H

#include <QWidget>
#include <QButtonGroup>
#include <QVBoxLayout>
#include <QAbstractButton>
#include <QRadioButton>

class ButtonGroup : public QWidget
{
    Q_OBJECT
public:
    explicit ButtonGroup(QWidget *parent = 0);

    Q_PROPERTY(int checkedId READ getCheckedId WRITE checkId USER true)
    int getCheckedId();
    void checkId(int id);

    void addButton(QAbstractButton *button, int id);

signals:
    void buttonClicked(int);

private:
    QButtonGroup *m_buttonGroup;
    QVBoxLayout *m_layout;


};

#endif // BUTTONGROUP_H

radiobuttondelegate.h(这里也没什么新鲜事):

#ifndef RADIOBUTTONDELEGATE_H
#define RADIOBUTTONDELEGATE_H

#include <QItemDelegate>

class RadioButtonDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    explicit RadioButtonDelegate(QObject *parent = 0);

signals:

public slots:

    void commitMyData();
};

#endif // RADIOBUTTONDELEGATE_H

radiobuttondelegate.cpp(或此处):

#include "radiobuttondelegate.h"
#include <QDebug>
RadioButtonDelegate::RadioButtonDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}
void RadioButtonDelegate::commitMyData() {
    QWidget *obj = qobject_cast<QWidget*>(sender());
    emit commitData(obj);
    emit closeEditor(obj);

    qDebug() << "commitMyData";
}