QT QTreeView单击双击事件,获取被单击的整行数据

 
//初始化表头
POSCollect::POSCollect(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
	 model = new QStandardItemModel(0, 5, parent);

	model->setHeaderData(0, Qt::Horizontal, QObject::tr("SOCKET编号"));
	model->setHeaderData(1, Qt::Horizontal, QObject::tr("IP地址"));
	model->setHeaderData(2, Qt::Horizontal, QObject::tr("端口"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("源定义"));
	model->setHeaderData(4, Qt::Horizontal, QObject::tr("编号"));
	ui.online_treeView->setRootIsDecorated(false);
	ui.online_treeView->setAlternatingRowColors(true);
	ui.online_treeView->setSortingEnabled(true);
    ui.online_treeView->setModel(model);
    
	connect(ui.online_treeView,SIGNAL(doubleClicked ( const QModelIndex)),this,SLOT(OnlineTreeViewDoubleClick(const QModelIndex)));





}

添加数据

void POSCollect::addOnline( const QString &id,
			 const QString &ip, const QString &port, const QString &mtype,const QString &mid)
{
	model->insertRow(0);
	model->setData(model->index(0, 0), id);
	model->setData(model->index(0, 1), ip);
	model->setData(model->index(0, 2), port);
	model->setData(model->index(0, 3), mtype);
	model->setData(model->index(0, 4), mid);
}


//双击并获取整列的文本数据

 

void POSCollect::OnlineTreeViewDoubleClick(const QModelIndex & index)
{

QAbstractItemModel* m=(QAbstractItemModel*)index.model();
for(int columnIndex = 0; columnIndex < m->columnCount(); columnIndex++)
{
	QModelIndex x=m->index(index.row(),columnIndex);

	QString s= x.data().toString();
	QMessageBox::about(this,s,s);
}
}

就这些代码,NND,花了我一天的时间,最后才在群里请教别人才搞定.巨汗...................

 


 

你可能感兴趣的:(c/c++)