PDA

View Full Version : QTextEdit SIGSEGV



Flayer
13th January 2010, 13:57
Hello, and sorry for being such a newb by asking, which I believe really is a simple thing.

Okay, here's the thing.
I want to do a simple editor, which then will check the input characters and change them to something else, and the
set them in the QTextEdit.

What I mean is that, when I write for eg. a 2, it will be checked against a list of characters, and if there's a match, change the 2 into
a specific text/chars.

I want it to change my 2 to a G#. But the QTextEdit shouldn't input characters that I don't want, eg. z, x etc.

And as you may already know, I am newb, just started with Qt yesterday :P
Im not a complete newb when it comes to C++, even tho it may seem so. I just have a hard time figuring out what needs to be done
and how to do it "right".

I've been looking through this forum, the Qt docs and google, but without success.
So, even if it's against my principles, I decided to ask you guys here at the forum.

When I execute my program, it gives me the SIGSEV signal when I try to edit the text in my QTextEdit.

The code looks like this atm:

dialog.h


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QLabel>
#include <QTextEdit>

class myDialog : public QDialog
{
Q_OBJECT

private:

QLineEdit* m_lneTitle;
QLabel* m_lTitle;
QTextEdit* m_document;

private slots:

void textEditSlot();

public:

myDialog();
virtual ~myDialog(){};
};

#endif // DIALOG_H



dialog.cpp


#include "dialog.h"

myDialog::myDialog()
{
//fönsterstorlek samt pos. vart den ska vara när app. startas
this->setGeometry(200, 100, 600, 400);

//skapa en label
m_lTitle = new QLabel(this);
m_lTitle->setText("Song Title:");
m_lTitle->setGeometry(20, 20, 80, 20);

// skapa lineedit, till songtiteln
m_lneTitle = new QLineEdit(this);
m_lneTitle->setGeometry(80, 20, 140, 20);

//skapa själva editrutan
m_document = new QTextEdit(this);
m_document->setGeometry(20, 60, 560, 300);

//koppla lite signaler
connect(m_document, SIGNAL(textChanged()), this, SLOT(textEditSlot()));
}

void myDialog::textEditSlot()
{
// haven't put anything here atm, cuz anything in here makes it crasch. with anything I mean like m_document->setText();
}


main.cpp

#include <QApplication>
#include "dialog.h"

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

myDialog* d = new myDialog();
d->show();

return app.exec();
}

I haven't put any code in the signal slot right now, cuz everything that I tried just crashed the program.

Is this because of the "SIGNAL(textChanged()"?
If so, what should I use instead?

Oh, and I could be satisfied with any solution, I just want to get this to work right now.
If it's possible to catch the keyboard event, which I assume is, how would I do that and then set the text in the QTextEdit via
that function?

The reason I want this to work is because I feel that me and my buddy, who happens to be a way better coder than me, created
an application in SDL which reads "notes" from a text file, and then output the sound via either the PC-speaker or via the speakers.

Now you may think: "Well, if your buddy is such a better coder than you, why doesn't he create this?".
Well, the answer to that is: "Because I want to learn more, I want to "grow"."

So, please. If you feel like helping me out here, it would be greatly appreciated.

/Flayer

high_flyer
13th January 2010, 16:56
Please refrain from all the non relevant explanations.
Just focus on the problem.

The widget code looks valid to me.
Can you show what code exactly makes your application crash?
Does it crash with the empty slot?

and there is no reason to allocate myDialog on the heap:



myDialog d;

d.show();

wysota
13th January 2010, 17:37
Consider this: in a slot which is executed every time text is changed in the widget you try to change the text. What do you think it leads to?

Flayer
14th January 2010, 22:18
Consider this: in a slot which is executed every time text is changed in the widget you try to change the text. What do you think it leads to?

Ye, thats what I thought, but what should I use instead?
How would I acheive the same thing without the "textChanged" or any other that "works" like it, it would just be the same thing.

wysota
14th January 2010, 22:52
I would connect to QTextDocument::contentsChange() signal to detect modifications to the document. In the slot I would check all modified blocks (based on the position) for strings I was searching for. If found, I would prepare a replacement for them. Finally I would make the replacements. You just have to remember to provide some means of making sure you won't fall into an infinite loop - either by making sure replacements and searches are disjoint (i.e. that no part of any replacement will trigger a search match) or by setting a flag "hey, I'm in the middle of replacing, don't fall into the loop". Also a nice although a bit more advanced technique would be to use QTextBlockUserData to mark areas that have been replaced by the mechanism (this would allow to preserve replacements even if they are not disjoint from searches).