0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
VOT Productions
  • 136
  • 1
  • 9

2 Answers2

1

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.

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35
0

You can acess the text edit directly with this:

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

Sir Digby Chicken Caesar
  • 3,063
  • 1
  • 23
  • 31