python tableview_QT tableview例子

孔驰
2023-12-01

#include "mainWindows.h"

QString chakan(QString path){

QDir dir(path);

QString new_name;

foreach(QFileInfo mfi ,dir.entryInfoList())

{

if(mfi.isFile())

{

new_name= mfi.fileName();

//  qDebug()<< "File :" << mfi.fileName();

}else

{

if(mfi.fileName()=="." || mfi.fileName() == "..")continue;

qDebug() << "Entry Dir" << mfi.absoluteFilePath();

chakan(mfi.absoluteFilePath());

}

}

return new_name;

}

QString read_result_head(const char *file_name)

{

QString line;

QString displayString;

QFile file(file_name);

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))

{

qDebug()<

}

QTextStream stream(&file);

// = in.readLine();

stream.seek(0);

while(!stream.atEnd())

{

printf("a\n");

line = stream.readLine();

displayString.append(line);

//  qDebug() << line;

break;

}

file.close();   //关闭文件

return displayString;

}

QString read_result_data(const char *file_name)

{

QString line;

QString displayString;

QFile file(file_name);

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))

{

qDebug()<

}

QTextStream stream(&file);

// = in.readLine();

stream.seek(0);

while(!stream.atEnd())

{

printf("a\n");

line = stream.readLine();

displayString.append(line);

//  qDebug() << line;

//  break;

}

file.close();   //关闭文件

return line;

}

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

{

std::string str;

char *path_name;

path_name=(char *)malloc(100);

char *str_data;

str_data=(char *)malloc(100);

//设置窗口大小

this->resize(480,272);

int r=0;

const char *abc;

QString temp;

QString head_name;

QTableView *table=new QTableView;

QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

table->setFont(QFont("wenquanyi", 6, QFont::Bold));

QVBoxLayout *pVBoxLayout = new QVBoxLayout(this);

pVBoxLayout->addWidget(table);

QStandardItemModel *model=new QStandardItemModel( 1, 13);

table->resize(480,272);

table->setColumnWidth(0, 85);

QString test_name;

test_name=chakan("/home/sinyd210/test/product_data");

test_name="/home/sinyd210/test/product_data/"+test_name;

//      test_name=chakan("/media/mmcblk0p1/test_log/");

//      test_name="/media/mmcblk0p1/test_log/"+test_name;

QByteArray product_result_name=test_name.toLatin1();

path_name=product_result_name.data();

qDebug()<< path_name;

temp=read_result_head(path_name);

for(int head=4;head<13;++head)

{

head_name=temp.section(',',head,head);

QByteArray ba=head_name.toAscii();

str_data=ba.data();

qDebug() <

abc =str_data;

model->setHeaderData(head, Qt::Horizontal, QObject::tr(abc));

}

//  str = temp.toStdString();

//

temp=read_result_data(path_name);

qDebug() << temp<

for(int row=0;row<10;++row)

{

for( r=0; r<13; ++r )

{

//   qDebug() << temp<

QStandardItem *item = new QStandardItem( QString(temp.section(',',r,r)).arg(r+1) );

model->setItem( row, r, item );

}

}

table->verticalHeader()->setDefaultSectionSize(25);

table->horizontalHeader()->setDefaultSectionSize(35);

table->setModel(model); // 正常设置模型,没有任何特殊之处

model->setHeaderData(0, Qt::Horizontal, QObject::tr("timestamp"));

model->setHeaderData(1, Qt::Horizontal, QObject::tr("product_id"));

model->setHeaderData(2, Qt::Horizontal, QObject::tr("duration"));

model->setHeaderData(3, Qt::Horizontal, QObject::tr("illegal"));

table->setStyleSheet("QTableView { border: none;"

"selection-background-color: #8EDE21;"

"color: black}");

// BarDelegate delegate;

//  table.setItemDelegateForColumn( 1, &delegate ); // 设置第一列为代理

//   this->setCentralWidget(table);

QColor(green);

table->show();

}

MainWindow::~MainWindow()

{

}

阅读(3564) | 评论(0) | 转发(0) |

 类似资料: