Qt项视图类

这部分东西,还是需要多敲才行。

testModel.h

#ifndef TESTMODEL_H
#define TESTMODEL_H

#include <QAbstractItemModel>

#include "Log.h"

class TestModel : public QAbstractItemModel
{
	Q_OBJECT

public:
	TestModel(QObject *parent);
	~TestModel();

	QVariant data(const QModelIndex &index, int role) const;
	QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;
	QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;
	QModelIndex parent(const QModelIndex &index) const;
	int rowCount(const QModelIndex &parent=QModelIndex()) const;
	int columnCount(const QModelIndex &parent=QModelIndex()) const;
	void push_back(Log* pLog);
	Qt::ItemFlags flags(const QModelIndex &index) const;
	bool setData(const QModelIndex &index, const QVariant &value, int role= Qt::EditRole );

	bool isSelected(Log* pLog) const;
public:
	void getSelectedLogs(QList<Log*>& logarray);
	QList<Log*> & getAllLog();

	void clearLog();

private:
	QList<Log*> m_items;
	QList<Log*> m_selectedItems;
};

#endif // TESTMODEL_H

cpp文件:

#include "testmodel.h"

#include <QColor>
#include <QPixmap>

//#include <QDateTime>

TestModel::TestModel(QObject *parent)
	: QAbstractItemModel(parent)
{

}

TestModel::~TestModel()
{

}

QVariant TestModel::data( const QModelIndex &index, int role ) const
{
	if(!index.isValid())
		return QVariant();
	QVariant var;
	Log* item = static_cast<Log*>(index.internalPointer());
	if (!item)
		return QVariant();
	//QDateTime tempTime = QDateTime::fromTime_t(0);
	if(role==Qt::DisplayRole)
	{
		switch(index.column())
		{
		case 0:
			var.setValue((index.row()+1));
			break;
		case 1:
			var.setValue(item->time);///.toString("yyyy-MM-dd hh:mm:ss"));
			break;
		case 2:
			var.setValue(item->level);
			break;
		case 3:
			var.setValue(item->info);
			break;
		}
	}      /Decoration : n.装饰,装潢;装饰品;装饰图案,装饰风格;奖章
	else if(role==Qt::DecorationRole)   
	{
		switch(index.column())
		{
		case 2:
			if(item->level==1)    ///Project-Resource Files-project name.qrc-u know?
			var.setValue(QPixmap(QString::fromUtf8(":/QTabViewTest/Resources/check_ok.png")));
			break;
		default:
			break;
		}
	}
	else if(role==Qt::CheckStateRole)
	{
		if(index.column()==4)
		{
			if(isSelected(item))
				return Qt::Checked;
			else
				return Qt::Unchecked;
		}
	}        ///Alignment:n.队列,排成直线; 结盟; 校直,调整; [工]准线
	else if(role==Qt::TextAlignmentRole)  
	{
		return Qt::AlignCenter;      ///居中显示
	}
	else if(role==Qt::BackgroundRole)
	{
		if(item->level==0 && index.column() == 3)
		{
			var.setValue(QColor(12,134,0));
		}
	}
	return var;
}

QVariant TestModel::headerData( int section, Qt::Orientation orientation, int role) const
{
	QVariant var;
	if(orientation==Qt::Horizontal)
	{
		if(role==Qt::DisplayRole)
		{
			switch(section)
			{
			case  0:
				var = tr("序号");
				break;
			case  1:
				var = tr("时间");
				break;
			case 2:
				var = tr("日志级别");
				break;
			case 3:
				var = tr("日志信息");
				break;
			case 4:
				var = tr("操作");
				break;
			}
		}
	}
	return var;
}

int TestModel::rowCount( const QModelIndex &parent/*=QModelIndex()*/ ) const
{
	return m_items.size();
}

int TestModel::columnCount( const QModelIndex &parent/*=QModelIndex()*/ ) const
{
	return 5;
}

Qt::ItemFlags TestModel::flags( const QModelIndex &index ) const
{
	if(!index.isValid())
		return 0;
	
	/*Qt::ItemFlags flag=QAbstractItemModel::flags(index);
	flag|=Qt::ItemIsEditable;
	return flag;*/
	Log *item = static_cast<Log*>(index.internalPointer());
	if(index.column()==4)
		return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QModelIndex TestModel::parent( const QModelIndex &index ) const
{
	return QModelIndex();
}

QModelIndex TestModel::index( int row, int column, const QModelIndex &parent ) const
{
	return hasIndex(row,column,parent) ? createIndex(row,column,m_items[row]) :QModelIndex();
}

void TestModel::push_back( Log* pLog )
{
	m_items.append(pLog);
}

bool TestModel::isSelected( Log* pLog ) const
{
	for(int i=0; i<m_selectedItems.size(); i++)
	{
		if(pLog==m_selectedItems[i])
			return true;
	}
	return false;
}

bool TestModel::setData( const QModelIndex &index, const QVariant &value, int role)
{
	if(!index.isValid())
		return false;

	Log *item=static_cast<Log*>(index.internalPointer());
	if(index.column()==4 && role==Qt::CheckStateRole)
	{
		if(value==Qt::Checked)    //选中
		{
			if(!isSelected(item))
				m_selectedItems.push_back(item);
		}
		else     ///取消选中操作
		{
			if(isSelected(item))
			{
				m_selectedItems.removeOne(item);
			}
		}
	}
	emit dataChanged(index,index);
	return true;
}

void TestModel::getSelectedLogs( QList<Log*>& logArray )
{
	logArray=m_selectedItems;
}

QList<Log*> & TestModel::getAllLog() 
{
	return m_items;
}

void TestModel::clearLog()
{
	for(int i=0; i<m_items.size(); i++)
	{
		Log *item=m_items[i];
		delete item;
	}
	m_items.clear();
}

mainwindow.h文件

#ifndef QTABVIEWTEST_H
#define QTABVIEWTEST_H

#include <QtGui/QMainWindow>
#include "ui_qtabviewtest.h"

#include "testmodel.h"
#include <QTableView>

class QTabViewTest : public QMainWindow
{
	Q_OBJECT

public:
	QTabViewTest(QWidget *parent = 0, Qt::WFlags flags = 0);
	~QTabViewTest();

public slots:
	void addLog();
	void pushLog(Log *pLog);
	void updateTabView();
	void refreshView();

private:
	QTableView *m_pTabView;
	TestModel *m_pTestModel;
	Ui::QTabViewTestClass ui;
};

#endif // QTABVIEWTEST_H

cpp文件

#include "qtabviewtest.h"
#include "Log.h"
#include "adddialog.h"

QTabViewTest::QTabViewTest(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	m_pTabView=new QTableView(this);
	m_pTestModel=new TestModel(this);

	m_pTabView->setModel(m_pTestModel);

	this->setCentralWidget(m_pTabView);

	refreshView();

	connect(ui.addAction,SIGNAL(triggered()),this,SLOT(addLog()));
}

QTabViewTest::~QTabViewTest()
{

}

void QTabViewTest::addLog()
{
	AddDialog *pAddDialog=new AddDialog;
	connect(pAddDialog,SIGNAL(addLog(Log*)),this,SLOT(pushLog(Log*)));
	pAddDialog->setWindowTitle(tr("增加日志"));
	if(pAddDialog->exec()==QDialog::Accepted)
	{
	}
}

void QTabViewTest::pushLog( Log *pLog )
{
	m_pTestModel->push_back(pLog);
	updateTabView();
}

void QTabViewTest::updateTabView()
{
	m_pTabView->setModel(NULL);
	m_pTabView->setModel(m_pTestModel);
	refreshView();
}

void QTabViewTest::refreshView()
{
	m_pTabView->setColumnWidth(0,50);
	m_pTabView->setColumnWidth(1,150);
	m_pTabView->setColumnWidth(2,60);
	m_pTabView->setColumnWidth(3,250);
	m_pTabView->setColumnWidth(4,100);
}

addDialog.cpp文件

#include "adddialog.h"
#include <QMessageBox>

AddDialog::AddDialog(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

//	connect(ui.okBtn,SIGNAL(clicked()),this,SLOT(doOK()));
	connect(ui.cancelBtn,SIGNAL(clicked()),this,SLOT(doCancel()));
	connect(ui.addBtn,SIGNAL(clicked()),this,SLOT(doAdd()));
}

AddDialog::~AddDialog()
{
}

//void AddDialog::doOK()
//{
//	QMessageBox::about(this,tr("note"),tr("ok"));
//	this->accept();
//}

void AddDialog::doCancel()
{
	this->close();
}

void AddDialog::doAdd()
{
	QDateTime time=QDateTime::currentDateTime();
	QString strTime=time.toString("yyyy-MM-dd hh:mm:ss");
	int nLevel=ui.levelLine->text().toInt();
	QString strInfo=ui.infoText->toPlainText();

	Log *pLog=new Log(strTime,nLevel,strInfo);
	emit addLog(pLog);
}

log文件

#pragma once

#include <QString>
#include <QDateTime>

class Log
{
public:
	Log(void);
	~Log(void);

	Log(QString time,int level,QString info);

	enum LogLevel
	{
		normal=0,
		warnning=1,
		error=2,
	};

	QString time;
	int level;
	QString info;
};
///2013-10-23     1      "can't find the data"

main

	QTextCodec *codec = QTextCodec::codecForName("GBK");
	QTextCodec::setCodecForLocale(codec);
	QTextCodec::setCodecForCStrings(codec);
	QTextCodec::setCodecForTr(codec);

截图如下:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值