I generate a file by extracting information from diffeent files and saving the data on each file to a line in the generated file . Then i read the file into my program and display it in QTableview. I created a QItemdelegate that adds a button to each row in my Qtableview. So at the click of the each of the button, I want a QDialog to pop up which should read the content of the matching file from which the contents of that row was generated. The name of the file, which is the content of the variable is what I want the program to use in locating the file.
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;
}
}
To extract the text that you are in the r-th
row and before the first comma you just have to iterate over the QTextStream
to locate row, then you separate the text according to the used separator, in your case the comma after you leave the loop.
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;
}