2016-09-24 88 views
0

如何識別標籤上的雙擊以更改其標籤? 最好我可以編輯標籤,但也可以從另一個輸入框中獲取字符串。有什麼建議麼? 的標籤被添加,目前指定的標籤,如:如何通過雙擊更改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(); 
} 

回答

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的幾何圖形設置爲正確的子選項(例如,不要覆蓋「side widgets 「一個標籤)。

僞隨機碼braindumped:

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 
+0

嗨,我想知道什麼'tabWidget->'是(在'連接()')..?我已經添加了方法中的代碼,該代碼將會話和標籤添加到** EDIT1 **原始帖子中** – cerr

+0

這是您擁有的QTabWidget。 – peppe