Question

I'm fairly new at C++/Qt, and I only have some web development experience.

I'm testing a few things with Qt for learning purposes and I'm failing miserably at it. I'm pretty sure it's because the whole slot/signal thing hasn't settled in yet, so I was hoping someone could make it clearer for me.

So, I have this main program UI where I have placed a QTextEdit widget. Now I'm trying to reproduce one of those "there are changes to the document, better save it!"-warnings, and it's being displayed everytime someone tries to create a new document.

I'm trying to test for changes in the textEdit widget when the "New Document" option is triggered. I keep getting these compile errors and I don't even know what they mean! What would be the correct test condition? How can I refer to the textEdit, since it's being called somewhere else?

I'm trying something like this:

void Notepad::on_actionNew_triggered()
{
    //not getting the test condition right!

    if(................................) {

    QMessageBox msgBox;
    msgBox.setText("Warning!");
    msgBox.setInformativeText("Changes were applied to this document.");
    msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Cancel);
    int ret = msgBox.exec();


        switch (ret) {
            case QMessageBox::Discard:
                // Don't Save was clicked
                ui->textEdit->clear();
                break;
            case QMessageBox::Cancel:
                msgBox.close();
                break;
            default:
                // should never be reached
                break;
        }
    }else{ui->textEdit->clear();}
}

I have tried searching some information about this, and I bet most of you might actually think this is pretty obvious, but I'm having real trouble understanding how to get around this.

Was it helpful?

Solution 2

So, I was trying to solve this and I decided to make a slot returning a bool value whenever the textEdit has suffered changes. As I figured out, I thought of using this as a test whenever the "New action" was triggered. So, after the test, I set the bool value back to false and it's working fine:

void Notepad::on_actionNew_triggered()
{    
    if(Notepad::on_textEdit_textChanged()) {
    ~Notepad::on_textEdit_textChanged(); 
    QMessageBox msgBox;
    msgBox.setText("Warning!");
    msgBox.setInformativeText("Changes were applied to this document.");
    msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Cancel);
    int ret = msgBox.exec();


        switch (ret) {
            case QMessageBox::Discard:
                // Don't Save was clicked
                ui->textEdit->clear();
                break;
            case QMessageBox::Cancel:
                msgBox.close();
                break;
            default:
                // should never be reached
                break;
        }
    }else{ui->textEdit->clear();}
}

bool Notepad::on_textEdit_textChanged()
{
    return true;
}

OTHER TIPS

So, you've got a few things in play here. One is when you're clicking New you need to perform a check to see if there's a document already existing that hasn't been saved, you're not far off by thinking of signal and slots for it.

So when you press New you need to send a signal to your document, if you've created a custom class this is easy as you can add a new slot dirtyDocument (for example!) which can relay the documents status back, for example by emitting another signal containing a bool flag and deal with it inside another slot. Or, in your on_actionNew_triggered slot you could ask the document object which is currently open whether it's got unsaved changes by passing a reference to the object, or maintaining a class reference to it (that's where you have something like Document *doc; in your notepad.h file).

If you're getting compile issues copy any information that doesn't have something specific to your code and paste it into Google. Odds are someone will have asked the same question, most probably on SO itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top