テキストの編集を手動で追跡する必要があります。それらへのポインタを親ウィジェットに格納するか、 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( QTabWidget* 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
また、を呼び出すと、適切なQTextEditを指さなくなる可能性がありQTabWidget::removeTab
ます。QHash