1

QTabWidgetのようなタブを追加する「tw」があります。

QWidget *newTab = new QWidget(tw);
tw->addTab(newTab, "Tab name");
QTextEdit *te = new QTextEdit();
te->setText("Hello world");
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addWidget(te);
newTab->setLayout(vbox);

フォアグラウンドにあるタブから内部のテキストを取得するにはどうすればよいですかQTextEdit(ボタンをクリックしたときに、表示されているタブからクリップボードまたはそのようなsmtgにテキストをコピーしたい場合など)。のハンドルを取得する方法がわかりませんQTextEdit

4

1 に答える 1

2

テキストの編集を手動で追跡する必要があります。それらへのポインタを親ウィジェットに格納するか、 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

于 2013-02-12T15:19:19.017 に答える