demo界面演示如下:
通过customContextMenuRequested(QPoint)信号实现鼠标右键事件,对应的槽函数为:
void QFtpWidget::customContextMenuRequestedSlot(QPoint pos) {
if (m_infoVec.empty()) {
return;
}
m_menu = new QMenu(ui.tableWidget);
//m_menu->exec(cursor().pos());
m_menu->move(cursor().pos());
m_menu->show();
int x = pos.x();
int y = pos.y();
QModelIndex index = ui.tableWidget->indexAt(QPoint(x,y));
m_crtRow = index.row();
m_crtCol = index.column();
if (m_crtRow < m_infoVec.size()) {
FileInfo info = m_infoVec[m_crtRow];
m_renameAct = new QAction("重命名", ui.tableWidget);
connect(m_renameAct, SIGNAL(triggered()), this, SLOT(renameSlot()));
m_menu->addAction(m_renameAct);
if (info.type == FileType::File) {
m_downloadAct = new QAction("下载", ui.tableWidget);;
connect(m_downloadAct, SIGNAL(triggered()), this, SLOT(downloadSlot()));
m_menu->addAction(m_downloadAct);
m_deleteAct = new QAction("删除", ui.tableWidget);
connect(m_deleteAct, SIGNAL(triggered()), this, SLOT(deleteSlot()));
m_menu->addAction(m_deleteAct);
}
else {
m_removeAct = new QAction("移除", ui.tableWidget);
connect(m_removeAct, SIGNAL(triggered()), this, SLOT(removeSlot()));
m_menu->addAction(m_removeAct);
}
}
}
通过commandFinished()信号实现FTP操作的解析,对应的槽函数实现:
void QFtpWidget::ftpCommandFinishedSlot(int i, bool error) {
if (ftp->currentCommand() == QFtp::ConnectToHost)
{
if (error) {
ui.textBrowser->append("FTP连接失败-----");
return;
}
else {
ui.textBrowser->append("FTP连接成功-----");
ui.connectBtn->setText("断开连接");
ui.selecFiletBtn->setEnabled(true);
// ui.filepathEt->setEnabled(true);
ui.preBtn->setEnabled(true);
ui.createBtn->setEnabled(true);
ftp->list();
}
}
else if (ftp->currentCommand() == QFtp::Close) {
if (error) {
ui.textBrowser->append("FTP断开失败-----");
}
else {
clearTableData();
m_infoVec.clear();
ui.textBrowser->append("FTP断开成功-----");
}
}
else if (ftp->currentCommand() == QFtp::List) {
//qDebug() << "ftp->currentCommand() == QFtp::List----------------";
updateTableWgtData();
}
else if (ftp->currentCommand() == QFtp::Put) {
if (error) {
ui.textBrowser->append("文件上传失败--------------------------");
}
else {
ui.textBrowser->append("文件上传成功--------------------------");
ftp->list();
m_infoVec.clear();
}
}
else if (ftp->currentCommand() == QFtp::Get) {
if (error) {
ui.textBrowser->append("文件下载失败--------------------------");
}
else {
m_file->close();
ui.textBrowser->append("文件下载成功--------------------------");
}
}
else if (ftp->currentCommand() == QFtp::Rename) {
if (error) {
ui.textBrowser->append("重命名失败--------------------------");
}
else {
ui.textBrowser->append("重命名成功--------------------------");
ftp->list();
m_infoVec.clear();
}
}
else if (ftp->currentCommand() == QFtp::Mkdir) {
if (error) {
ui.textBrowser->append("创建文件夹失败--------------------------");
}
else {
ui.textBrowser->append("创建文件夹成功--------------------------");
}
}
else if (ftp->currentCommand() == QFtp::Remove) {
if (error) {
ui.textBrowser->append("删除文件失败--------------------------");
}
else {
ui.textBrowser->append("删除文件成功--------------------------");
ftp->list();
m_infoVec.clear();
}
}
else if (ftp->currentCommand() == QFtp::Rmdir) {
if (error) {
ui.textBrowser->append("移除文件夹失败--------------------------");
}
else {
ui.textBrowser->append("移除文件夹成功--------------------------");
ftp->list();
m_infoVec.clear();
}
}
}
上传功能的函数为:
void QFtpWidget::on_selecFiletBtn_clicked() {
QString str = "打开文件";
QString filepath = QFileDialog::getOpenFileName(this, str);
if (filepath.isEmpty()) {
return;
}
QString filename = filepath.split("/").takeLast();
QFile* file = new QFile(filepath, this);
filename = QString::fromLatin1(filename.toLocal8Bit());
ftp->put(file, filename);
}
下载功能函数为:
void QFtpWidget::downloadSlot() {
if (m_crtRow < m_infoVec.size()) {
FileInfo info = m_infoVec[m_crtRow];
if (info.type == FileType::File) {
QString path = QFileDialog::getExistingDirectory();
path.append("/").append(info.filename);
m_file = new QFile(path);
if(m_file->open(QIODevice::ReadWrite | QIODevice::Append) ){
path = info.filename;
ftp->get(QString::fromLatin1(info.filename.toLocal8Bit()), m_file);
}
}
}
}
重命名功能函数为:通过renameSlot设置可编辑状态,通过itemSelectionChangedSlot实现rename功能,同时关闭编辑状态。
void QFtpWidget::renameSlot() {
if (m_crtRow < m_infoVec.size()) {
FileInfo info = m_infoVec[m_crtRow];
m_oldName = info.filename;
m_isRenamed = true;
QTableWidgetItem* item = ui.tableWidget->item(m_crtRow, m_crtCol);
ui.tableWidget->setCurrentCell(m_crtRow, m_crtCol);
ui.tableWidget->openPersistentEditor(item);
ui.tableWidget->editItem(item);
}
}
void QFtpWidget::itemSelectionChangedSlot() {
if (m_isRenamed) {
m_isRenamed = false;
m_newName = ui.tableWidget->item(m_crtRow, m_crtCol)->text();
ui.tableWidget->closePersistentEditor(ui.tableWidget->item(m_crtRow, m_crtCol));
ftp->rename(QString::fromLatin1(m_oldName.toLocal8Bit()), QString::fromLatin1(m_newName.toLocal8Bit()));
}
}
删除文件功能函数为:
void QFtpWidget::deleteSlot() {
if (m_crtRow < m_infoVec.size()) {
QString name = m_infoVec[m_crtRow].filename;
ftp->remove(QString::fromLatin1(name.toLocal8Bit()));
}
}
删除文件夹功能函数为:
void QFtpWidget::removeSlot() {
if (m_crtRow < m_infoVec.size()) {
QString name = m_infoVec[m_crtRow].filename;
ftp->rmdir(QString::fromLatin1(name.toLocal8Bit()));
}
}
新建文件夹功能函数为:
void QFtpWidget::on_createBtn_clicked() {
if (ftp->state()!= QFtp::LoggedIn) {
return;
}
FileInfo info;
int row = ui.tableWidget->rowCount();
//qDebug() << "row---------------------" << row;
ui.tableWidget->insertRow(row);
ui.tableWidget->setRowHeight(row, 30);
QTableWidgetItem* btItem = new QTableWidgetItem(); //靶体
btItem->setTextAlignment(Qt::AlignLeft); //文字居中
btItem->setFlags(btItem->flags() & (~Qt::ItemIsEditable));
btItem->setIcon(QIcon("folder.png"));
QString datatime = QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm");
info.type = FileType::Folder;
info.filename = datatime;
btItem->setText(datatime);
ui.tableWidget->setItem(row, 0, btItem);
QTableWidgetItem* btItem1 = new QTableWidgetItem();
btItem1->setTextAlignment(Qt::AlignHCenter); //文字居中
btItem1->setText("");
info.size = "";
ui.tableWidget->setItem(row, 1, btItem1);
btItem1->setFlags(btItem1->flags() & (~Qt::ItemIsEditable));
QTableWidgetItem* btItem2 = new QTableWidgetItem();
btItem2->setTextAlignment(Qt::AlignHCenter); //文字居中
datatime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm");
info.time = datatime;
btItem2->setText(datatime);
ui.tableWidget->setItem(row, 2, btItem2);
btItem2->setFlags(btItem2->flags() & (~Qt::ItemIsEditable));
m_infoVec.push_back(info);
ftp->mkdir(QString::fromLatin1(info.filename.toLocal8Bit()));
}
动态效果展示为:
版权声明:本文为weixin_41377572原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。