我知道如果我用手写的话我可以使用for循环,但我怎么能用Qt设计器呢?或者我应该将这两种方式混合在一起以达到目标?
THX
答案 0 :(得分:0)
当我尝试实现Recent Files时,我提出了这样的解决方案:
我在启动时创建menù项目并将它们设置为不可见:
separatorAct = ui.menu_File->addSeparator();
for (int i = 0; i < MAXRECENTFILE; ++i) {
RecentProjects[i] = new QAction(this);
RecentProjects[i]->setVisible(false);
connect(RecentProjects[i], SIGNAL(triggered()), this, SLOT(OpenRecentFile()));
ui.menu_File->addAction(RecentProjects[i]);
}
然后我更新Recent Files条目(存储在文件中):
void BulbCalculator::UpdateRecentFileActions() {
QSettings settings("GRYS","BulbCalculator");;
QStringList files = settings.value("RecentProject").toStringList();
int numRecentFiles = qMin(files.size(), MAXRECENTFILE);
for (int i = 0; i < numRecentFiles; ++i) {
QString text = tr("&%1 %2").arg(i + 1).arg(StrippedName(files[i]));
RecentProjects[i]->setText(text);
RecentProjects[i]->setData(files[i]);
RecentProjects[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MAXRECENTFILE; ++j) {
RecentProjects[j]->setVisible(false);
}
separatorAct->setVisible(numRecentFiles > 0);
}
插槽OpenRecentFile
非常简单地实现
void BulbCalculator::OpenRecentFile() {
QAction *action = qobject_cast<QAction *>(sender());
if (action) {
this->BcStatus->CurFile = action->data().toString();
LoadFile();
//LoadFile(action->data().toString());
}
}
希望这可以提供帮助
答案 1 :(得分:0)
我创建了一个类,用于解析QMainWindow菜单栏上的文本项“
标题:
#pragma once
#include <QObject>
#include <list>
#include <vector>
class QMainWindow;
class QAction;
class GFQtRecentFileMenu : public QObject {
Q_OBJECT
public:
explicit GFQtRecentFileMenu(QObject* parent = nullptr);
void updateRecentFileList(QMainWindow* pMW, const std::vector<QString>& files);
void removeAddedEntries();
private:
std::list<QAction*> m_AddedActions;
std::vector<QString> m_filenames;
public slots:
void onRmuMenuItem();
};
实现:
#include "gfqtrecentfilemenu.h"
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include "gfappframework.h"
class QMenuBarIterator {
QMenuBar* i_barP;
QMenu* m_RMU_menu;
QAction* m_RMU_action;
bool investigate(QAction* pA) {
if (pA->text() == QString("<RMU>")) { // HERE'S THE MENU TEXT PLACEHOLDER
m_RMU_action = pA;
return true;
}
return false;
}
bool iterate_sub(QMenu* m) {
for (QAction* action: m->actions()) { // sub menus of File, Edit, ...
if (investigate(action)) {return true;}
if (action->menu()) {
if (iterate_sub(action->menu())) {
m_RMU_menu = action->menu();
return true;
}
}
}
return false;
}
public:
QMenuBarIterator(QMenuBar* barP) : i_barP(barP), m_RMU_menu(nullptr), m_RMU_action(nullptr) {}
bool iterate() {
QList<QMenu*> menuBar = i_barP->findChildren<QMenu*>();
for (QMenu* m: menuBar) { // File, Edit, ..., Help
if (investigate(m->menuAction()) || iterate_sub(m)) {
m_RMU_menu = m;
return true;
}
}
return false;
}
QAction* action() {return m_RMU_action;}
QMenu* menu() {return m_RMU_menu;}
};
GFQtRecentFileMenu::GFQtRecentFileMenu(QObject* parent) : QObject(parent) {
}
void GFQtRecentFileMenu::updateRecentFileList(QMainWindow* pMW, const std::vector<QString>& files) {
removeAddedEntries();
m_filenames = files;
QMenuBarIterator mbi(pMW->menuBar());
if (mbi.iterate()) {
QAction* pA = mbi.action();
auto* pmenu = mbi.menu();
pA->setVisible(false); // hide the dummy, but keep in place. We're adding menu items at this position!
int index = 0;
for (auto& f: files) {
QAction* pA = pmenu->addAction(f);
pA->setData(index++);
m_AddedActions.push_back(pA);
connect(pA, SIGNAL(triggered()), this, SLOT(onRmuMenuItem()));
}
}
}
void GFQtRecentFileMenu::removeAddedEntries() {
for (auto* a : m_AddedActions) { delete a; }
m_AddedActions.clear();
}
void GFQtRecentFileMenu::onRmuMenuItem() {
auto* action= qobject_cast<QAction*>(sender());
if (action != nullptr) {
int index = action->data().toInt();
if (index >= 0 && index < int(m_filenames.size())) {
// open the document now - however you do that...
GetMyApplication().openDocument(m_filenames[index]);
}
qDebug() << m_filenames[i] << " clicked";
}
}