User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Qt4/C++ - QTextEdit subclass - can't show cursor

Sun Apr 05, 2015 12:04 pm

Hello,

I'm subclassing a QTextEdit and I can't get the cursor to show.

Flow:-
From the button slot, I call a QDialog (propForm).
From the dialog, I call a subclassed QTextEdit (data/dataEdit).

No matter what I try, I can't get the cursor to show (visible).
The program compiles and runs OK, up to the point of a noshow cursor.

Could someone tell me what I haven't done.

Regards

Code: Select all

void menu::textButton() // slot
{
    prop = new propForm(this);
    prop->setObjectName("prop" + QString::number(cm::formCount));
    prop->setFocus();
}

Code: Select all

#ifndef PROPFORM_H
#define PROPFORM_H

#include <QDialog>
#include <QLabel>
#include "dataEdit.h"

class propForm : public QDialog
{
    Q_OBJECT

public:
    propForm(QWidget *parent = 0);
    dataEdit *data;
    QLabel *fileData;

protected slots:
    void focusInEvent(QFocusEvent *);
    void closeEvent(QCloseEvent *);
    void focusOutEvent(QFocusEvent *);
    bool eventFilter(QObject *, QEvent *);
};
#endif // PROPFORM_H

Code: Select all

propForm::propForm(QWidget *parent) : QDialog(parent)
{
    this->resize(400, 300);
    this->setWindowTitle(cm::subItem);
    this->setStyleSheet(sty::style1);

    QString filePath = (cm::mainItem + "/" + cm::subItem);

    fileData = new QLabel(filePath, this);
    fileData->setGeometry(0, 0, this->width(), 25);
    fileData->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    fileData->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);

    data = new dataEdit(this);
    data->setObjectName("data" + QString::number(cm::formCount));
    data->setGeometry(0, fileData->height(), this->width(), this->height() - fileData->height());

    QFile file(filePath);
    if ( ! (file.open(QIODevice::ReadOnly))) {
        cr::message("Can't open file\nfor read.", "Error", 2);
        return;
    }

    QTextStream in(&file);
    while ( ! in.atEnd()) data->append(in.readLine());
    file.close();

    this->show();
}

Code: Select all

#ifndef DATAEDIT_H
#define DATAEDIT_H

#include <QTextEdit>

class dataEdit : public QTextEdit
{
    Q_OBJECT

public:
    dataEdit(QWidget *parent = 0);
    ~dataEdit();

protected slots:
    void focusInEvent(QFocusEvent *);
    void closeEvent(QCloseEvent *);
    void focusOutEvent(QFocusEvent *);
    bool eventFilter(QObject *, QEvent *);
    void showEvent(QShowEvent *);
    void resizeEvent(QResizeEvent *e);

private slots:
    void onTextChanged();

private:
    //QTextCursor *textCursor;
};

#endif // DATAEDIT_H

Code: Select all

dataEdit::dataEdit(QWidget *parent) : QTextEdit(parent)
{
    qDebug() << "start1";
    this->activateWindow();
    this->setFocus();

    connect(this, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
    qDebug() << "start2";

    this->setReadOnly(false);
    this->setEnabled(true);
    this->setLineWrapMode(NoWrap);
    QTextCursor textCursor(this->textCursor());
    textCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
    textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor, 1); //end of first line
    this->ensureCursorVisible();
    qDebug() << "end";
}

Code: Select all

void dataEdit::showEvent(QShowEvent *e)
{
    qDebug() << "show1";
}

Code: Select all

void dataEdit::focusInEvent(QFocusEvent *)
{
    qDebug() << "focusInEvent - " << this->objectName();
}

Code: Select all

void dataEdit::onTextChanged()
{
    qDebug() << "text changed";
}

Code: Select all

OUTPUT:
start1 
start2 
end 
text changed [text changed
text changed (from
text changed (the data
text changed (loaded
text changed (at startup
show1 
focusInEvent -  "data1"

Return to “C/C++”