Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spell check doesn't work #6

Closed
tox-user opened this issue Jun 17, 2018 · 5 comments
Closed

Spell check doesn't work #6

tox-user opened this issue Jun 17, 2018 · 5 comments

Comments

@tox-user
Copy link

I'm using version 0.8.4 (a8f702b) on Debian and I can't get spell check to work. When I input words into a QTextEdit it doesn't highlight misspelled words. I have the needed dictionaries installed. Here is my code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QtSpell.hpp>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // create a QtSpell::TextEdit instance
    QtSpell::TextEditChecker checker;

    // attach to a QTextEdit or QPlainTextEdit
    QTextEdit* textEdit = new QTextEdit(this);
    checker.setTextEdit(textEdit);
}
@manisandro
Copy link
Owner

Does the example [1] work? Do you get any messages printed on the terminal?

[1] https://github.com/manisandro/qtspell/tree/master/examples

@tox-user
Copy link
Author

That worked! The issue was with how TextEditChecker is initialized. This line from readme file doesn't work:

// create a QtSpell::TextEdit instance
QtSpell::TextEditChecker checker;

needs to be replaced with:

QtSpell::TextEditChecker* checker = new QtSpell::TextEditChecker(this);

tox-user added a commit to tox-user/qtspell that referenced this issue Jun 18, 2018
@manisandro
Copy link
Owner

manisandro commented Jun 18, 2018

I don't really think stack vs heap allocation makes a difference, unless your checker is going out of scope when stack allocated. This works fine:

#include <QApplication>
#include <QTextEdit>
#include <QtSpell.hpp>

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);

    QtSpell::TextEditChecker checker;
    QTextEdit textEdit;
    checker.setTextEdit(&textEdit);
    checker.setDecodeLanguageCodes(true);
    checker.setShowCheckSpellingCheckbox(true);
    checker.setUndoRedoEnabled(true);

    textEdit.show();

    return app.exec();
}

@tox-user
Copy link
Author

That works too. So textEdit can't be a pointer?

@manisandro
Copy link
Owner

The relevant thing is not whether something is stack or heap allocated, but whether you correctly handle the life-time of an object. I.e.

void foo() {
    QTextEdit stackEdit;
    QTextEdit* heapEdit = new QTextEdit();
    // Stack allocated stackEdit is destroyed when scope it was declared it ends
    // Heap allocated heapEdit lives beyond the scope of `foo`, until explicitly deleted
 }

If you not familiar with object lifetime and stack vs heap allocation, and specifically when dealing with QObject inherited classes, I recommend you read up on those topics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants