Normal People Scare Me
Normal People Scare Me

Reputation: 841

Qt access TextEdit's text in other scope

I'm trying to create a simple IDE. At the moment I'm totally failing! I have some actions and when I click on the action "new" it should add a new page to my TabWidget and create a TextEdit inside. This actually works but I do not know how to use the TextEdit. For example if I want to save it, I need access to the text of the textEdit but how?

Code:

void MainWindow::on_actionNew_triggered()
{
    QTextEdit* textEdit = new QTextEdit();
    ui->Tabs->addTab(textEdit, "New Tab");
    ui->Tabs->setCurrentIndex( ui->Tabs->count() );
}

Upvotes: 1

Views: 407

Answers (1)

typ1232
typ1232

Reputation: 5607

You need some variable that is out of the scope to be able to access it.

Your textEdit is defined in local scope of the function. If you want to access to for example on global space of the cpp file you could define it as

static QTextEdit *textEdit;

outside of any function.

A better idea may be to define it as member variable of the MainWindow class:

private:
    QTextEdit *m_textEdit;

Remember that memory allocated with new is allocated until you delete it.

Upvotes: 1

Related Questions