Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 每个代理的非平凡qt模型数据操作_C++_Qt - Fatal编程技术网

C++ 每个代理的非平凡qt模型数据操作

C++ 每个代理的非平凡qt模型数据操作,c++,qt,C++,Qt,正如我开始问的那样,我试图在不同的视图中以不同的方式查看Qt中单个模型中包含的数据,而不必创建更多的模型,也不必更改它 更具体地说:我正在尝试获取一个QString,并将其显示在几个表中,一个字母一个字母地显示,在其他情况下,操作被认为是简单的 命名一个这样的操作:一个表将反转字符串(该表只有一行,但由于与程序相关的原因,行编辑在这里不起作用),并删除任何空格(这些空格将保留在模型中,因为它们在其他视图中是必需的) 编辑:给出一个更完整、更具体的例子:Hello World->Dlrow Oll

正如我开始问的那样,我试图在不同的视图中以不同的方式查看Qt中单个模型中包含的数据,而不必创建更多的模型,也不必更改它

更具体地说:我正在尝试获取一个QString,并将其显示在几个表中,一个字母一个字母地显示,在其他情况下,操作被认为是简单的

命名一个这样的操作:一个表将反转字符串(该表只有一行,但由于与程序相关的原因,行编辑在这里不起作用),并删除任何空格(这些空格将保留在模型中,因为它们在其他视图中是必需的)

编辑:给出一个更完整、更具体的例子:Hello World->Dlrow Olleh。 我想反转大写字母,检查是否有完全由大写字母组成的单词-这些单词将完全保持大写

另一个计划中的操作是删除所有空格,但这次表将是一个矩阵(因此我猜需要对索引函数进行一些更改)

EDIT2:请注意,矩阵的大小将根据输入的大小而改变,以适应屏幕,因此索引(至少在表本身中)将不会保持不变。如果这很重要的话

在过去的两天里,我一直在试图弄清楚如何做到这一点,我能找到的最好的方法(除了阅读Qt文档之外,这是没有帮助的)

然而,它并没有解释每段代码都做得足够好,因此不允许像我这样已经不理解这个概念的人对代码进行任何修改以满足我的需要

当然,我可以创建更多的模型,并在混乱的底部之外执行所有这些操作,但我喜欢挑战自己,尽可能多学习一些东西,并找到更好、更有效的方法来做事情

在对QabStretcProxyModel进行子类化时,我需要在QabStretcProxyModel中实现什么?更重要的是,我应该如何实现它(非常感谢解释代码)
为了创建提供此行为的代码?(我知道每个代理可能会有所不同)

除非您用所需的各种行为的必要测试用例来修改问题,这里有一个简单的代理,它可以反转字符串值并去掉所有空格:

class FlexibleProxy : public QIdentityProxyModel {
public:
  enum {
    StripSpaces = 0x1,
    Reverse = 0x2
  } Operation;
  Q_DECLARE_FLAGS(Operations, Operation)
private:
  Operations m_ops;
public:
  FlexibleProxy(Operations ops, QObject * parent = 0) :
    QIdentityProxyModel(parent), m_ops(ops) {}
  QVariant data(QModelIndex & index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE {
    auto val = QIdentityProxyModel::data(index, role);
    if ((role == Qt::DisplayRole || role == Qt::EditRole) 
        && val.userType() == QMetaType::QString) {
      auto str = val.toString();
      if (m_ops & StripSpaces) str.remove(QLatin1Char(' '));
      if (m_ops & Reverse) reverse(str);
      val.setValue(str);
    }
    return val;
  }
};
Q_DECLARE_OPERATORS_FOR_FLAGS(FlexibleProxy::Operations)

static void reverse(QString & str) {
  auto const size = str.size();
  for (int i = 0; i <= size/2; ++i) {
    std::swap(str[i], str[size-i]);
  }
}

由于您不需要数据过滤(根据您的操作),因此可以使用
QIdentityProxyModel
。下面是所有的课程和截图

mainwindow.cpp

#include "mainwindow.h"
#include "basemodel.h"
#include "identitymodel.h"

#include <QTableView>
#include <QBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget* cWidget = new QWidget(this);
    setCentralWidget(cWidget);

    IdentityModel* normalModel = new IdentityModel(IdentityModel::UnModified, cWidget);
    IdentityModel* reverseModel = new IdentityModel(IdentityModel::Reverse, cWidget);
    IdentityModel* noSpaceModel = new IdentityModel(IdentityModel::NoSpace, cWidget);

    normalModel->setSourceModel(&BaseModel::instance());
    reverseModel->setSourceModel(&BaseModel::instance());
    noSpaceModel->setSourceModel(&BaseModel::instance());

    QTableView* view1 = new QTableView(cWidget);
    QTableView* view2 = new QTableView(cWidget);
    QTableView* view3 = new QTableView(cWidget);

    view1->setModel(normalModel);
    view2->setModel(reverseModel);
    view3->setModel(noSpaceModel);

    QHBoxLayout* hLayout = new QHBoxLayout(cWidget);
    hLayout->addWidget(view1);
    hLayout->addWidget(view2);
    hLayout->addWidget(view3);
}
#包括“mainwindow.h”
#包括“basemodel.h”
#包括“identitymodel.h”
#包括
#包括
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
QWidget*cWidget=新的QWidget(此);
setCentralWidget(cWidget);
IdentityModel*normalModel=newidentitymodel(IdentityModel::未修改,cWidget);
IdentityModel*reverseModel=newidentitymodel(IdentityModel::Reverse,cWidget);
IdentityModel*noSpaceModel=newidentitymodel(IdentityModel::NoSpace,cWidget);
normalModel->setSourceModel(&BaseModel::instance());
reverseModel->setSourceModel(&BaseModel::instance());
noSpaceModel->setSourceModel(&BaseModel::instance());
QTableView*view1=新的QTableView(cWidget);
QTableView*view2=新的QTableView(cWidget);
QTableView*view3=新的QTableView(cWidget);
view1->setModel(normalModel);
view2->setModel(反向模型);
view3->setModel(noSpaceModel);
QHBoxLayout*hLayout=新的QHBoxLayout(cWidget);
hLayout->addWidget(视图1);
hLayout->addWidget(视图2);
hLayout->addWidget(视图3);
}

basemodel.h

    #ifndef BASEMODEL_H
    #define BASEMODEL_H

    #include <QStringListModel>

    class BaseModel : public QStringListModel
    {
        Q_OBJECT

    public:

        static BaseModel& instance()
        {
            static BaseModel sInstance;
            return sInstance;
        }

        virtual int rowCount(const QModelIndex& parent) const
        {
            Q_UNUSED(parent);

            return m_dataList.size();
        }

        virtual QVariant data(const QModelIndex& modelIndex, int role) const;

    signals:

    public slots:

    private:

        explicit BaseModel(QObject* prnt=0);

        QStringList         m_dataList;
    };

    #endif // BASEMODEL_H
\ifndef BASEMODEL\u H
#定义基本模型
#包括
类BaseModel:公共QStringListModel
{
Q_对象
公众:
静态BaseModel和实例()
{
静态基础模型;
回归承诺;
}
虚拟整数行数(常量QModelIndex&parent)常量
{
Q_未使用(父母);
返回m_dataList.size();
}
虚拟QVariant数据(常量QModelIndex和modelIndex,int角色)常量;
信号:
公众时段:
私人:
显式基本模型(QObject*prnt=0);
QStringList m_数据列表;
};
#endif//BASEMODEL\u H
basemodel.cpp

    #include "basemodel.h"

    BaseModel::BaseModel(QObject* prnt)
        : QStringListModel(prnt),
          m_dataList({"Hello World", "Good Bye World"})
    {
    }

    QVariant BaseModel::data(const QModelIndex& modelIndex, int role) const
    {
        if (modelIndex.isValid() && modelIndex.row() < m_dataList.size() && role == Qt::DisplayRole) {
            return m_dataList.at(modelIndex.row());
        }
        return QVariant();
    }
#包括“basemodel.h”
BaseModel::BaseModel(QObject*prnt)
:QStringListModel(prnt),
m_数据列表({“你好世界”,“再见世界”})
{
}
QVariant BaseModel::数据(常量QModelIndex和modelIndex,int角色)常量
{
if(modeleindex.isValid()&&modeleindex.row()

identitymodel.h

#define IDENTITYMODEL_H

#include <QIdentityProxyModel>

class IdentityModel : public QIdentityProxyModel
{
    Q_OBJECT

public:

    enum StringMod
    {
        UnModified,
        Reverse,
        NoSpace
    };

    explicit IdentityModel(StringMod mod, QObject* prnt=0);

    virtual QVariant data(const QModelIndex& proxyIndex, int role) const;

signals:

public slots:

private:

    StringMod       m_stringMod;
};
#定义IDENTITYMODEL#H
#包括
类标识模型:公共QIdentityProxyModel
{
Q_对象
公众:
枚举StringMod
{
未经修改,
相反,
NoSpace
};
显式标识模型(StringMod mod,QObject*prnt=0);
虚拟QVariant数据(常量QModelIndex和proxyIndex,int角色)常量;
信号:
公众时段:
私人:
StringMod m_StringMod;
};

identitymodel.cpp

#include "identitymodel.h"    
#include <algorithm>

IdentityModel::IdentityModel(StringMod mod, QObject* prnt)
    : QIdentityProxyModel(prnt),
      m_stringMod(mod)
{
}

QVariant IdentityModel::data(const QModelIndex& proxyIndex, int role) const
{
    QVariant dataToModify(QIdentityProxyModel::data(proxyIndex, role));
    if (role == Qt::DisplayRole && m_stringMod != UnModified) {
        QString str(dataToModify.toString());
        if (m_stringMod == NoSpace)
            return str.remove(' ');
        else {
            QByteArray ba(str.toUtf8());
            std::reverse(ba.data(), ba.data() + str.length());
            str = QString(ba);
        }
        return str;
    }
    return dataToModify;
}
#包括“identitymodel.h”

#包括

所以,你有一个
QString
,说“你好,世界”。您希望在一个表中显示“dlroW olleH”,在另一个表中显示“HelloWorld”。我说得对吗?这是一个过于简单化的问题(我会做更多),但这是基本的想法。这个问题缺乏必要的细节。请通过提供您打算进行的实际修改的示例对其进行修改。描述它们的正式方式是编写一系列
Q_ASSERT
s:一个用于原始模型上
数据的值,另一个用于代理合成的项。例如,对于字符串反转和剥离I assu
#include "identitymodel.h"    
#include <algorithm>

IdentityModel::IdentityModel(StringMod mod, QObject* prnt)
    : QIdentityProxyModel(prnt),
      m_stringMod(mod)
{
}

QVariant IdentityModel::data(const QModelIndex& proxyIndex, int role) const
{
    QVariant dataToModify(QIdentityProxyModel::data(proxyIndex, role));
    if (role == Qt::DisplayRole && m_stringMod != UnModified) {
        QString str(dataToModify.toString());
        if (m_stringMod == NoSpace)
            return str.remove(' ');
        else {
            QByteArray ba(str.toUtf8());
            std::reverse(ba.data(), ba.data() + str.length());
            str = QString(ba);
        }
        return str;
    }
    return dataToModify;
}