您需要手动跟踪文本编辑。通过将指针存储在父窗口小部件中,或者可以使用查找表(例如,一个QHash:
假设你有一个类MyClass
其中包含了你的问题发布的代码:
添加一个成员变量是这样的:
class QTextEdit; // this is a so-called "Forward Declaration" which saves you an
// #include. Google it if you want to know more ;-)
class MyClass
{
// ...
private:
QHash< int, QTextEdit* > _textEditPerTabPage;
};
这个变量能够存储(和找到)标签页索引(0,1,2,...)中的文本编辑。
你可以做一个附加功能是这样的:
void MyClass::addTab(QWidget* tabWidget, const QString& tabName, const QString& text)
{
// Create the text edit
QTextEdit* textEdit = new QTextEdit();
textEdit->setText(text);
// Create a layout which contains the text edit
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(textEdit);
// Create a parent widget for the layout
QWidget* newTab = new QWidget(tabWidget);
newTab->setLayout(layout);
// Add that widget as a new tab
int tabIndex = tabWidget->addTab(newTab, tabName);
// Remember the text edit for the widget
_textEditPerTabPage.insert(tabIndex, textEdit);
}
,然后检索在QTextEdit
这样的指针:
QTextEdit* textEdit = _textEditPerTabPage.value(tabWidget->currentIndex());
此代码有一些限制,例如中您必须确保使用您自己的MyClass::addTab
功能,并且不要在该功能之外访问QTabWidget::addTab
。此外,如果您致电QTabWidget::removeTab
,您的QHash
可能不再指向正确的QTextEdits。
'te-> toPlaintText()'? – hank 2013-02-12 14:53:57
我需要一个句柄才能做到这一点。我没有一个。我所知道的是,包含它的标签在前台。 – 2013-02-12 14:54:34
因此,将您的'QTextEdit'添加为包含您的'QTabWidget'的小部件的成员。 – hank 2013-02-12 14:56:30