linux书签上下移动
步骤
读书签文件,分割到qstringlist,点击右键菜单获取序号,交换序号,写书签文件。
qstringlist.swap(int, int) 过时,qstringlist.swapitemsat(int, int) 代替。
书签文件路径
- mainwindow.h
const qstring filepath_bookmark = qstandardpaths::writablelocation(qstandardpaths::configlocation) + "/gtk-3.0/bookmarks";
上移
connect(action_bookmark_move_up, &qaction::triggered, [=](){
qtreewidgetitem *twi = ui->treewidget_nav->currentitem();
int index = twi_bookmark->indexofchild(twi);
qfile file(filepath_bookmark);
if (file.open(qfile::readwrite)) {
qtextstream ts(&file);
qstring s = ts.readall();
qstringlist sl = s.split("\n");
sl.swapitemsat(index, index - 1);
s = "";
for (int i=0; i<sl.length(); i++) {
s += sl.at(i);
if (i < sl.length() - 1)
s += "\n";
}
file.resize(0);
ts << s;
file.close();
}
});下移
- swap -1 改成 +1,其他一样。
connect(action_bookmark_move_down, &qaction::triggered, [=](){
qtreewidgetitem *twi = ui->treewidget_nav->currentitem();
int index = twi_bookmark->indexofchild(twi);
qfile file(filepath_bookmark);
if (file.open(qfile::readwrite)) {
qtextstream ts(&file);
qstring s = ts.readall();
qstringlist sl = s.split("\n");
sl.swapitemsat(index, index + 1);
s = "";
for (int i=0; i<sl.length(); i++) {
s += sl.at(i);
if (i < sl.length() - 1)
s += "\n";
}
file.resize(0);
ts << s;
file.close();
}
});监视到书签文件变化更新书签树
qfilesystemwatcher只触发一次
- 百度ai回答:
- 只能触发一次,如果需要持续触发,需要在处理完触发后,再次添加路径。
const qstring filepath_bookmark = qstandardpaths::writablelocation(qstandardpaths::configlocation) + "/gtk-3.0/bookmarks";
qfilesystemwatcher *watcher = new qfilesystemwatcher;
watcher->addpath(filepath_bookmark);
connect(watcher, &qfilesystemwatcher::filechanged, [=]{
genbookmark();
watcher->addpath(filepath_bookmark);
});总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论