【问题标题】:Is there a method or a way to check whether a user has visited a certain tab page on a QTabWidget?有没有一种方法或方法可以检查用户是否访问了 QTabWidget 上的某个标签页?
【发布时间】:2019-02-01 14:59:37
【问题描述】:

我的应用程序上有一个 QTabWidget,因此用户可以通过单击标题来浏览标签页,我想知道用户何时打开标签页,他/她之前是否访问过该页面。在 QWizard 类中有一个方法 hasVisitedPage() 可以在向导上执行完全相同的操作,但我找不到类似的方法在 QTabWidget 类中。我想知道的是,在 QWizard 中是否有这样的方法?

这是QWizard类http://doc.qt.io/archives/qt-4.8/qwizard.html#hasVisitedPage中的类似方法

目前我正在使用 QList 来存储访问过的页面索引,并且每次当用户打开标签页时检查 QList 是否包含打开页面的索引,我认为如果我有一个方法会更容易检查

【问题讨论】:

    标签: c++ qt qtabwidget


    【解决方案1】:

    我想知道的是,在QWizard中是否有这样的方法?

    很遗憾,没有。

    目前我正在使用 QList 来存储访问过的页面索引,并且每次用户打开标签页时检查 QList 是否包含打开页面的索引

    QWizard 做同样的事情,即有一个QList<int> history; 属性。所以,在我看来你的做法是正确的

    查看source code 了解更多详情。特别是,QWizardPrivate::switchToPage 可能会让您感兴趣,以便让您了解它是如何在 QWizard 中完成的,因此您可以检查自己的实现并在必要时进行调整。 p>

    【讨论】:

    • 非常感谢您的帮助
    【解决方案2】:

    history 属性很容易添加:

    // https://github.com/KubaO/stackoverflown/tree/master/questions/tabwidget-history-52033092
    #include <QtWidgets>
    #include <array>
    
    static const char kHistory[] = "history";
    
    auto getHistory(const QTabWidget *w) {
       return w->property(kHistory).value<QList<int>>();
    }
    
    void addHistory(QTabWidget *tabWidget) {
       QObject::connect(tabWidget, &QTabWidget::currentChanged, [tabWidget](int index) {
          if (index < 0) return;
          auto history = getHistory(tabWidget);
          history.removeAll(index);
          history.append(index);
          tabWidget->setProperty(kHistory, QVariant::fromValue(history));
       });
       if (tabWidget->currentIndex() >= 0)
          tabWidget->setProperty(
              kHistory, QVariant::fromValue(QList<int>() << tabWidget->currentIndex()));
    }
    
    bool hasVisitedPage(const QTabWidget *w, int index) {
       return getHistory(w).contains(index);
    }
    
    int main(int argc, char *argv[]) {
       QApplication app(argc, argv);
       QWidget ui;
       QVBoxLayout layout{&ui};
       QTabWidget tabWidget;
       QLabel history;
       layout.addWidget(&tabWidget);
       layout.addWidget(&history);
       std::array<QLabel, 5> tabs;
       for (auto &l : tabs) {
          auto const n = &l - &tabs[0] + 1;
          l.setText(QStringLiteral("Label on Page #%1").arg(n));
          tabWidget.addTab(&l, QStringLiteral("Page #%1").arg(n));
       }
       addHistory(&tabWidget);
       auto showHistory = [&] {
          auto text = QStringLiteral("History: ");
          for (auto i : tabWidget.property("history").value<QList<int>>())
             text.append(QString::number(i + 1));
          history.setText(text);
       };
       showHistory();
       QObject::connect(&tabWidget, &QTabWidget::currentChanged, showHistory);
       tabWidget.currentChanged(tabWidget.currentIndex());
       ui.show();
       return app.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 2013-09-28
      • 2021-06-27
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 2021-08-27
      • 2022-11-23
      相关资源
      最近更新 更多