如何在C ++中的Qt Creator中向TableView添加自定义对象?

时间:2018-01-29 15:04:49

标签: c++ qt uitableview qt-creator

我在Qt Creator中用默认数据写了一个简单的tableview。任何人都可以帮我如何将自定义数据对象设置到列中吗?

我还将连续插入和删除数据到表中。无论如何在没有指定行号和列号的情况下执行此操作?

我对Qt非常陌生。给出的所有示例都显示将数据添加到相应行和相应列。我在JAVAFx中编写了这个Tableview,很容易从表中存储和删除数据。

我的主要代码:

#include "mainwindow.h" 
#include "mydata.h"
#include <QApplication>
#include <QVariant>
#include <QTableView>
#include <QTreeView>

int main(int argc, char *argv[])

{
    QApplication a(argc, argv);
    mydata *data=new mydata;
    QVariant variant;
    variant.setValue(data);
    QStandardItemModel *model = new QStandardItemModel();
    QStandardItem *parentItem = model->invisibleRootItem();
    QStandardItem *item = new QStandardItem();
    model->setColumnCount(5);
    model->setRowCount(5);
    model->setData(model->index(0, 0), "data1");
    model->setData(model->index(0, 1), "data2");
    model->setData(model->index(0, 2), "data3");
    model->setData(model->index(0, 3), "data4");
    model->setData(model->index(0, 4), "data5");
    QTreeView *treeView = new QTreeView();
    QTableView *tableView = new QTableView();
    tableView->setModel(model);
    item->setText(QString("some data"));
    tableView->show();
    return a.exec();
}

我的自定义对象:

#include "mydata.h" 
#include <string>

mydata::mydata()

{
    Account="finsoldts5";
    Exchange="CME";
    Orderstatus="Filled";
    Clorid="CME";
}

我想将“mydata”类对象附加到表中,如: 第1列包含帐户,第2列包含Exchange,第3列包含Orderstatus,最后一列包含Clordid。

每次获取这些详细信息后,如何动态执行此操作。那就是我想在每次获取新数据时将对象添加到Table的最后一行。一个完整的例子是值得赞赏的,因为我对CPP和Qt都很陌生。

2 个答案:

答案 0 :(得分:1)

您需要为对象创建getter,例如..

String mydata::getAccount(){
      .....
      return Account;
}

然后使用像..

这样的东西
QString account = QString("%1").arg(getAccount());
model->setData(model->index(0,0), account);

答案 1 :(得分:0)

最后在朋友的帮助下做到了这一点。使用以下代码。我已经扩展了QAbstractModel并创建了我的自定义模型现在我拿了一个QList并将我的数据添加到它,然后我将这些数据添加到模型中,之后将模型添加到Tableview中。

<强> Custom.cpp

#include "custom.h" 
Custom :: Custom(){
}
//setters
void Custom :: set_Account(QString account)
{
    Account = account;
}
void Custom:: set_Exchange(QString exchange)
{
    Exchange =exchange;
}
void Custom:: set_Clordid(QString clordid)
{
    Clorid=clordid;
}
void Custom:: set_Orderstatus(QString orderstatus)
{
    Orderstatus=orderstatus;
}
void Custom:: set_Symbol(QString symbol)
{
    Symbol=symbol;
}
void Custom:: set_Quantity(QString quantity)
{
    Quantity=quantity;
}
void Custom::set_Price(QString price)
{
    Price=price;
}
void Custom::set_Side(QString side)
{
    Side=side;
}
//Getters
QString Custom::get_Account()
{
    return Account;
}
QString Custom::get_Exchange()
{
    return Exchange;
}
QString Custom::get_Clorid()
{
    return Clorid;
}
QString Custom::get_Orderstatus()
{
    return Orderstatus;
}
QString Custom::get_Symbol()
{
    return Symbol;
}
QString Custom::get_Quantity()
{
    return Quantity;
}
QString Custom::get_Price()
{
    return Price;
}
QString Custom::get_Side()
{
    return Side;
}

<强> custommodelofcustomobject.cpp

#include "custommodelofcustomobject.h" 
#include <QColor>
#include <QBrush>
#include <QFont>
CustomModelOfCustomObject::CustomModelOfCustomObject()
{
}
CustomModelOfCustomObject::CustomModelOfCustomObject(QObject *parent)
    : QAbstractTableModel(parent)
{
}
int CustomModelOfCustomObject::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return list.size();
}
int CustomModelOfCustomObject::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 7;
}
QVariant CustomModelOfCustomObject::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    if (index.row() >= list.size() || index.row() < 0)
        return QVariant();
    switch(role){
    case Qt::BackgroundColorRole:
    {
        return QVariant(QBrush (QColor(Qt::white)));
    }
    case Qt::TextAlignmentRole:
    {
        return QVariant ( Qt::AlignVCenter | Qt::AlignHCenter );
    }
    case Qt::DisplayRole:
    {
        Custom* obj = list.at(index.row());
        switch(index.column())
        {
        case 0:
            return obj->get_Account();
        case 1:
            return obj->get_Exchange();
        case 2:
            return obj->get_Clorid();
        case 3:
            return obj->get_Orderstatus();
        case 4:
            return obj->get_Symbol();
        case 5:
            return obj->get_Quantity();
        case 6:
            return obj->get_Price();
        case 7:
            return obj->get_Side();
        default:
            return false;
        }//displayrole switch
    }
    }// role switch
    return QVariant();
}
QVariant CustomModelOfCustomObject::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    if(role==Qt::FontRole)
    {
        // return QVariant(QBrush (QColor(Qt::white)));
        return QFont("Courier New", 24, QFont::Bold);
    }
    if (orientation == Qt::Horizontal) {
        switch (section) {
        case 0:
            return tr("Account");
        case 1:
            return tr("Exchange");
        case 2:
            return tr("Clordid");
        case 3:
            return tr("Orderstatus");
        case 4:
            return tr("Symbol");
        case 5:
            return tr("Quantity");
        case 6:
            return tr("Price");
        case 7:
            return tr("Side");
        default:
            return QVariant();
        }
    }
    return QVariant();
}
bool CustomModelOfCustomObject::insertRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginInsertRows(QModelIndex(), position, position+rows-1);
    for (int row=0; row < rows; row++) {
        Custom* obj;
        list.insert(position, obj);
    }
    endInsertRows();
    return true;
}
bool CustomModelOfCustomObject::removeRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginRemoveRows(QModelIndex(), position, position+rows-1);
    for (int row=0; row < rows; ++row) {
        list.removeAt(position);
    }
    endRemoveRows();
    return true;
}
bool CustomModelOfCustomObject::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        int row = index.row();
        Custom* p = list.value(row);
        /*if (index.column() == 0)
            p->set_Account(value.toString());
        else if (index.column() == 1)
            p->set_Exchange(value.toString());
        else
            return false;*/
        switch(index.column())
        {
        case 0:
            p->set_Account(value.toString());
        case 1:
            p->set_Exchange(value.toString());
        case 2:
            p->set_Clordid(value.toString());
        case 3:
            p->set_Orderstatus(value.toString());
        case 4:
            p->set_Symbol(value.toString());
        case 5:
            p->set_Quantity(value.toString());
        case 6:
            p->set_Price(value.toString());
        case 7:
            p->set_Side(value.toString());
        default:
            return false;
        }
        list.replace(row, p);
        emit(dataChanged(index, index));
        return true;
    }
    return false;
}
QList<Custom*> CustomModelOfCustomObject::getList()
{
    return list;
}
void CustomModelOfCustomObject :: setList(QList<Custom *> clist){
    list = clist;
}

主要课程

#include "mainwindow.h" 
#include <QApplication>
#include "custom.h"
#include <QDebug>
#include "custommodelofcustomobject.h"
#include <QTableView>
#include <QHeaderView>
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QList<Custom*> list;
for(int i=0;i<100;i++)
{
   Custom *custom = new Custom() ;
   QString account = "OrdeGateway";
   QString exchange = "CME";
   custom->set_Account(account);
   custom->set_Exchange(exchange);
   custom->set_Clordid("Clordid");
   custom->set_Orderstatus("Pending");
   custom->set_Symbol("ES");
   custom->set_Price("1000");
   custom->set_Side("BUY");
   custom->set_Quantity("10");
   list.append(custom);
 }
CustomModelOfCustomObject *model = new CustomModelOfCustomObject();
model->setList(list);
QTableView *tableView = new QTableView();
tableView->setModel(model);
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableView->show();
 return a.exec();
}