I have a QTableView *tableView. When an user chooses the files / folders in tableView and right click -> choose "Print these items", I want my program to print those names into a text file or assign to a string. How can I do that? Thank you.
frmainwindow.h:
private slots:
void showContextMenuRequested(QPoint pos);
frmainwindow.cpp:
#include "frmainwindow.h"
#include "ui_frmainwindow.h"
FrMainWindow::FrMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FrMainWindow)
{
ui->setupUi(this);
model1->setRootPath("c:\\");
ui->tableView->setModel(model1);
connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT (showContextMenuRequested(QPoint)));
}
FrMainWindow::~FrMainWindow()
{
delete ui;
}
void FrMainWindow::showContextMenuRequested(QPoint pos)
{
QMenu *contextMenu = new QMenu(this);
contextMenu->addAction(new QAction("Print these items", this));
contextMenu->popup(ui->tableView->viewport()->mapToGlobal(pos));
}
First of all, connect your action to a processing slot:
QAction* action = new QAction("Print these items", this);
connect(action, SIGNAL(triggered(), this, SLOT(printItems())));
Then you can access selected indexes tableView->selectionModel()->selectedIndexes()
and using these indexes access data model1->data(index)
:
void printItems()
{
QFile file(QLatin1String("file.txt"));
file.open(QIODevice::WriteOnly);
QModelIndexList indexes = ui->tableView->selectionModel()->selectedIndexes();
foreach (QModelIndex index, indexes)
{
file.write(model1->data(index).toString().toLatin1());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.