Well, i'm working on a text Editor. I'm still a little nooby with C++. I'm trying to create a Goto Line Command. Would someone give me an example of how to do it?

Thanks :P

Well. If you mean what i've done of all project. I just started, i'm doing the Window, Save, etc.

It depends on how the text in the editor is stored, but I am going to assume you are using 1 giant buffer. In that case, you would loop through the characters and every time you hit a newline character you would check to see if you reached the line requested by the user, or if there is no more characters in the buffer.

What I mean is that no one is going to do your work for you. So if you show us something you've done and ask "How do I go about adding feature X to it," someone might help you; but if--as you did--you say "I want feature X in my program; pleae do it for me," you're not going to get very far.

Depends completely on how you designed the program, what constitutes a line, how the command is entered. I can think of 10 ways it could be coded and none of them are compatible.

How much design work have you done so far? An editor is not something you can just start coding.

Oh, yea. I'm using Qt too. :P

main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ScriptEditor Editor;
    Editor.setWindowTitle(QObject::tr("Pokémon Online Scripting Editor"));
    Editor.show();
    return a.exec();
}

Part of Edit.cpp, it's long the edit.cpp :/

#include <QtGui>
#include "Editor.h"

ScriptEditor::ScriptEditor(QWidget *parent) : QPlainTextEdit(parent)
{
    lineArea = new LineArea(this);
    connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineWidth(int)));
    connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateNumbersArea(QRect, int)));
    connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
    updateLineWidth(0);
    highlightCurrentLine();
}

Edit.h

#ifndef EDITOR_H
#define EDITOR_H

#include <QPlainTextEdit>
#include <QObject>
#endif // EDITOR_H

class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;

class LineArea;

class ScriptEditor : public QPlainTextEdit
{
    Q_OBJECT
public:
    ScriptEditor(QWidget *parent = 0);
    void lineNumberPaint(QPaintEvent *event);
    int lineNumberWidth();
protected:
    void resizeEvent(QResizeEvent *event);
private slots:
    void updateLineWidth(int blockCount);
    void highlightCurrentLine();
    void updateLineArea(const QRect &, int);
private:
    QWidget *lineArea;
};

class LineArea : public QWidget
{
public:
    LineArea(ScriptEditor *editor) : QWidget(editor) {
        scriptEditor = editor;
    }
    QSize sizeHint() const {
        return QSize (scriptEditor->lineNumberWidth(), 0);
    }
protected:
    void paintEvent(QPaintEvent *event) {
        scriptEditor->lineNumberPaint(event);
    }

private:
    ScriptEditor *scriptEditor;
};

Edited 5 Years Ago by AnonymousX: n/a

I am not familiar with Qt, but I am guessing that there is some function inherited from GPlainTextEdit that will allow you to access the text in the editor. You need to look up some documentation for a function like that. Also you should put the #endif at the end of the file, not just after the includes.

This article has been dead for over six months. Start a new discussion instead.