VOT Productions
VOT Productions

Reputation: 146

QTabWidget - How do I edit each widget separately?

I am in the process of learning Qt and generally C++, and I am stuck on this problem. Hopefully, it will be simple to answer.

I have a QTextEdit created by this code:

void MainWindow::on_actionNewtab_triggered()
{
    ui->tabWidget->addTab(new QTextEdit, "Untitled Document");
}

Now, if I make three tabs, I'm not sure how to edit the QTextBox of the current tab that is selected. I looked in the documentation (which is pretty good) and I think I need this, but I'm not sure how. I also found this on this site, but that seems to edit all QTextEdits?

The answer is probably extremely simple and I'm just missing something :). Please let me know if you need more details.

Upvotes: 0

Views: 114

Answers (2)

Oleksii Shmalko
Oleksii Shmalko

Reputation: 3778

Save pointer to your desired QTextBox and edit it as you wish.

QTextEdit *myTextEdit;

void MainWindow::on_actionNewtab_triggered()
{
    myTextEdit = new QTextEdit;
    ui->tabWidget->addTab(myTextEdit, "Untitled Document");
}

You may also use QTabWidget::widget() and QTabWidget::currentWidget() to get pointer to your widget after creation, but you have to cast the result to actual type first.

Upvotes: 1

Sir Digby Chicken Caesar
Sir Digby Chicken Caesar

Reputation: 3133

You can acess the text edit directly with this:

qobject_cast<QTextEdit*>(ui->tabWidget->currentWidget())->SetText("my text");

Upvotes: 0

Related Questions