C++ 读取文件并保存行和列的内容,并将其作为变量保存在Qt c++;
我通过从不同的文件中提取信息并将每个文件上的数据保存到生成的文件中的一行来生成一个文件。然后我将文件读入我的程序,并在QTableview中显示它。我创建了一个QItemdelegate,在我的Qtableview中的每一行添加一个按钮。所以,在点击每个按钮时,我希望弹出一个QDialog,它应该读取生成该行内容的匹配文件的内容。文件名,即变量的内容,是我希望程序在定位文件时使用的名称C++ 读取文件并保存行和列的内容,并将其作为变量保存在Qt c++;,c++,qt,qt5,qfile,C++,Qt,Qt5,Qfile,我通过从不同的文件中提取信息并将每个文件上的数据保存到生成的文件中的一行来生成一个文件。然后我将文件读入我的程序,并在QTableview中显示它。我创建了一个QItemdelegate,在我的Qtableview中的每一行添加一个按钮。所以,在点击每个按钮时,我希望弹出一个QDialog,它应该读取生成该行内容的匹配文件的内容。文件名,即变量的内容,是我希望程序在定位文件时使用的名称 void MyDelegate::paint(QPainter *painter, const QStyleO
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 70;//the X coordinate
y = r.top();//the Y coordinate
w = 70;//button width
h = 30;//button height
button.rect = QRect(x,y,w,h);
button.text = "View log";
button.state = QStyle::State_Enabled;
QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
}
bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
Q_UNUSED(model)
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent * e = (QMouseEvent *)event;
int clickX = e->x();
int clickY = e->y();
QRect r = option.rect;//getting the rect of the cell
int x,y,w,h;
x = r.left() + r.width() - 30;//the X coordinate
y = r.top();//the Y coordinate
w = 30;//button width
h = 30;//button height
if( clickX > x && clickX < x + w )
if( clickY > y && clickY < y + h )
{
int r = index.row(); // <---- row
int c = index.column(); // <---- column
QFile file("/home/uboho/monitor_test_module/logs/jobSummary");
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
// file line counter
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
// read one line from textstream(separated by "\n")
QString fileLine = in.readLine();
// parse the read line into separate pieces(tokens) with "," as the delimiter
QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts);
// load parsed data to model accordingly
if (lineToken.size() == 6)
{
for (int row=r; int column=0; column< 2; column++;)
{
QStandardItem *item = new QStandardItem(lineToken[column]);
//mytablemodel3->setItem(Max_Number_of_Lines, column, item);
}
}
QDialog * d = new QDialog();
d->setGeometry(300,0,100,100);
d->show();
d->setWindowTitle("Log viewer");
}
}
}
return true;
}
}
void MyDelegate::paint(QPainter*painter,const QStyleOptionViewItem&option,const QModelIndex&index)const
{
QSTYLEOPTION按钮;
QRect r=option.rect;//获取单元格的rect
int x,y,w,h;
x=r.left()+r.width()-70;//x坐标
y=r.top();//y坐标
w=70;//按钮宽度
h=30;//按钮高度
button.rect=QRect(x,y,w,h);
button.text=“查看日志”;
button.state=QStyle::state\u已启用;
QApplication::style()->drawControl(QStyle::CE_按钮和按钮,油漆工);
}
bool MyDelegate::editorEvent(QEvent*事件,qabstractemmodel*模型,常量QStyleOptionViewItem&option,常量QModelIndex&index)
{
Q_未使用(型号)
如果(事件->类型()==QEvent::MouseButtonRelease)
{
QMouseEvent*e=(QMouseEvent*)事件;
int clickX=e->x();
int clickY=e->y();
QRect r=option.rect;//获取单元格的rect
int x,y,w,h;
x=r.left()+r.width()-30;//x坐标
y=r.top();//y坐标
w=30;//按钮宽度
h=30;//按钮高度
如果(单击x>x&&clickXy&&clickYshow();
d->setWindowTitle(“日志查看器”);
}
}
}
返回true;
}
}
要提取位于r-th
行中且在第一个逗号之前的文本,您只需在QTextStream
上迭代以定位行,然后根据使用的分隔符分隔文本,在您的情况下,在离开循环后使用逗号
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton button;
QRect r = option.rect;//getting the rect of the cell
button.rect = QRect(r.topRight() - QPoint(70, 0), QSize(70, 30));
button.text = "View log";
button.state = QStyle::State_Enabled;
QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
}
bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
Q_UNUSED(model)
if( event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent * e = (QMouseEvent *)event;
QPoint p = e->pos();
QRect r = option.rect;//getting the rect of the cell
QRect re(r.topRight() - QPoint(30, 0), QSize(30, 30));
if(re.contains(p)){
int r = index.row(); // <---- row
int c = index.column(); // <---- column
QString filename; // text of the first column and r-row
QFile file("/home/uboho/monitor_test_module/logs/jobSummary");
int counter = 0;
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
QString fileLine = in.readLine();
if(counter == r){
filename = fileLine.split(",", QString::SkipEmptyParts).first();
break;
}
counter++;
}
}
QDialog * d = new QDialog();
d->setGeometry(300,0,100,100);
d->show();
d->setWindowTitle("Log viewer");
}
}
return true;
}
void MyDelegate::paint(QPainter*painter,const QStyleOptionViewItem&option,const QModelIndex&index)const
{
QSTYLEOPTION按钮;
QRect r=option.rect;//获取单元格的rect
button.rect=QRect(r.topRight()-QPoint(70,0),QSize(70,30));
button.text=“查看日志”;
button.state=QStyle::state\u已启用;
QApplication::style()->drawControl(QStyle::CE_按钮和按钮,油漆工);
}
bool MyDelegate::editorEvent(QEvent*事件,qabstractemmodel*模型,常量QStyleOptionViewItem&option,常量QModelIndex&index)
{
Q_未使用(型号)
如果(事件->类型()==QEvent::MouseButtonRelease)
{
QMouseEvent*e=(QMouseEvent*)事件;
QPoint p=e->pos();
QRect r=option.rect;//获取单元格的rect
QRect-re(r.topRight()-QPoint(30,0),QSize(30,30));
如果(关于包含(p)){
int r=index.row();//show();
d->setWindowTitle(“日志查看器”);
}
}
返回true;
}
您能更具体地说明问题所在吗?您需要帮助的位置?您指的是哪一行和哪一列?是否将文件内容保存在与您按下的代理对应的项目中?如果是,您想保存所有内容还是只保存部分文本?如果它是文本的一部分,您如何识别该部分?我通过从不同的文件中提取信息并将每个文件上的数据保存到生成的文件中的一行来生成一个文件。然后我将文件读入我的程序,并在QTableview中显示它。我创建了一个QItemdelegate,在我的Qtableview中的每一行添加一个按钮。所以,在点击每个按钮时,我希望弹出一个QDialog,它应该读取生成该行内容的匹配文件的内容。文件名,即变量的内容,是我希望程序在定位文件时使用的名称。欢迎使用SO。请复习。非常感谢,那么我如何在Qdialog上显示内容呢。文件所在的文件夹部分是/home/uboho/monitor_test_module/logs/runninglogs/@BlavidaUbee。您必须创建一个自定义QDialog,其中包含一个QTextEdit或QPlainTextEdit,您可以在其中放置文本。默认情况下q对话框没有这个特性,但是C++使用变量来搜索文件夹中的匹配文件是我需要的,我已经创建了单击按钮时弹出的对话框,您可以在代码中看到它。请帮助优化代码以搜索文件并显示在对话框上。感谢you@BlavidaUbee考虑到文件名为filename,且目录是您最初注释的目录,则完整路径为:QString fullPath=QDir(“/home/uboho/monitor\u test\u module/logs/runninglogs/”)。filePath(文件名)代码>非常感谢您的支持