gen_next
gen_next

Reputation: 97

Access to QTextEdit widget from function

I've got Qt application in VS2012 plugin. As a result (of below code) a new window is created each time when my_slot_to_execute() function checks it's internal if condition. In Qt Designer I've got QTextEdit widget, where I would like see those results ( not each time in new window).

Could someone please help me how should I proceed ( or set access to this QTextEdit widget) from my_slot_to_execute() function to see each time results (which will be each time incremented) only in one window?

ProgramExample::ProgramExample(QWidget *parent): QMainWindow(parent)  // constructor
{
    ui.setupUi(this);

    // here are other part of working code

}

//and then

void ProgramExample::on_listView_clicked(const QModelIndex &index)
{
   // here are other part of working code
    my_slot_to_execute();
}

my_slot_to_execute()
{
    smatch matches;
    regex pattern("key(\\d{3}\\w{1})"); 
    string text;

    if ((some condition))
        {
        QTextEdit *textEdit = new QTextEdit;
        QString outputString1 = QString::fromStdString(matches[1]);
        textEdit->setText(QString("%1:").arg(outputString1));
        textEdit->show();
        }
}

Thanks in advance!

Upvotes: 1

Views: 1370

Answers (2)

phyatt
phyatt

Reputation: 19112

Say you have dragged in Qt Designer, a QTextEdit. It will give it an object name such as textEdit.

Then in your code, to reuse this element, you will put:

ui->textEdit->append("new text to append");

An alternative that doesn't use Qt Designer, is to go to the header of your Widget or MainWindow and put a member variable pointer to your QTextEdit (e.g. QTextEdit * m_textEdit;). Then in your constructor initialize it and put it in a layout and set the layout in your central widget or on your widget itself.

Then for the entire life of the widget, you can just call

m_textEdit->append("new text to append");

Hope that helps.

Upvotes: 0

Nemanja Boric
Nemanja Boric

Reputation: 22157

Well, just don't create new QTextEdit when you want to update its text:

void ProgramExample::my_slot_to_execute()
{
    smatch matches;
    regex pattern("key(\\d{3}\\w{1})"); 
    string text;

    if ((some condition))
        {
        QString outputString1 = QString::fromStdString(matches[1]);
        ui.textEdit->setText(QString("%1:").arg(outputString1));
        }
}

Of course, my_slot_to_execute function must be a member of ProgramExample class. If, it is in another class, you can pass pointer to QTextEdit object:

void ProgramExample::on_listView_clicked(const QModelIndex &index)
{
   // here are other part of working code
    my_slot_to_execute(ui.textEdit);
}

void SomeClass::my_slot_to_execute(QTextEdit* textEdit)
    {
        smatch matches;
        regex pattern("key(\\d{3}\\w{1})"); 
        string text;

        if ((some condition))
            {
            QString outputString1 = QString::fromStdString(matches[1]);
            textEdit->setText(QString("%1:").arg(outputString1));
            }
    }

Upvotes: 1

Related Questions