如何在双击时更改QTabWidget的label属性?

时间:2016-09-24 17:40:30

标签: qt kde qtabwidget

如何识别标签上的双击以更改其标签? 我可以在适当的位置编辑标签,但也可以从另一个输入框中获取一个字符串。有什么建议? 选项卡将被添加,目前指定的标签如下:

QString tab_label = QString("Shell (") + QString::number(session->id(), 16) + ")";
    addTab(session->widget(), tab_label);

我希望能够在创建后编辑标签。

哦,我应该在这里提一下,我也是一个Qt新手!

EDIT1
完整的方法:

int SessionStack::addSession(Session::SessionType type)
{
    Session* session = new Session(type, this);
    connect(session, SIGNAL(titleChanged(int,QString)), this, SIGNAL(titleChanged(int,QString)));
    connect(session, SIGNAL(terminalManuallyActivated(Terminal*)), this, SLOT(handleManualTerminalActivation(Terminal*)));
    connect(session, SIGNAL(activityDetected(Terminal*)), m_window, SLOT(handleTerminalActivity(Terminal*)));
    connect(session, SIGNAL(silenceDetected(Terminal*)), m_window, SLOT(handleTerminalSilence(Terminal*)));
    connect(session, SIGNAL(destroyed(int)), this, SLOT(cleanup(int)));

    m_sessions.insert(session->id(), session);

    QString tab_label = QString("Shell (") + QString::number(session->id(), 16) + ")";
    addTab(session->widget(), tab_label);

    emit sessionAdded(session->id());

    raiseSession(session->id());

    return session->id();
}

1 个答案:

答案 0 :(得分:2)

有一个QTabBar :: tabBarDoubleClicked信号,您只需将其连接到插槽即可检测到双击。此外,您还需要一些小部件来实际编辑选项卡的文本。如果你想要它“不合适”(比如,你打开一个对话框),那么它应该足以做类似的事情:

connect(tabWidget->tabBar(), &QTabBar::tabBarDoubleClicked,
        this, MyWidget::editTabBarLabel);

void MyWidget::editTabBarLabel(int tabIndex)
{
    if (tabIndex < 0)
        return;
    // open dialog asking for the new label,
    // f.i. via QInputDialog::getText

    // set the new label bakc
}

如果你需要一些就地修改,你需要或多或少地大量修改QTabBar才能这样做。

最简单的选项是在右侧选项卡上打开QLineEdit。要通过QTabBar获取选项卡的几何图形:.tabrect,以便可以将线编辑放在相同的几何图形中。您很可能不会在该路径上做到这一点(见下文),您需要将QTabBar子类化并使用initStyleOption作为给定的选项卡,然后将lineedit的几何图形设置为右下角(例如,不要覆盖标签的“侧面小部件”。)

Random pseudo braindumped code:

void MyTabBar::editTabBarLabel(int tabIndex)
{
    if (tabIndex < 0)
        return;

    if (!m_lineEdit) {
        m_lineEdit = new QLineEdit(this);
        // once done, commit the change, or abort it, anyhow
        // destroy/hide (?) the line edit
        connect(m_lineEdit, &QLineEdit::editingFinished,
                this, &MyTabBar::renameLabel);
    } else {
        // we're actually editing something else, what to do?
        // commit the other change and start editing here?
    }

    m_editedTabIndex = tabIndex; // remember which one we're editing
    m_lineEdit->setText(tabText(tabIndex));     

    // not "entirely" accurate, you need better subrect control here,
    // cf. QStyle and https://doc.qt.io/qt-5/style-reference.html#widget-walkthrough
    // that's why this should really be a QTabBar subclass, because
    // you'll need to invoke initStyleOption and then fetch the subrects from the style

    m_lineEdit->setGeometry(tabRect(tabIndex));
    m_lineEdit->show();
}

// also track resize of the tabbar, relayout, tab reorder, tab insertion and removal, etc.
// move the QLineEdit accordingly