PDA

View Full Version : Contest



fullmetalcoder
25th January 2006, 09:44
Sorry but has there's no Contests section in the forum I decided to post it here!

I would like to start a contest( nothing to gain except fame :D ) :
Anybody able to make a fast QTextEdit whose each line can be fully colored (I mean the background coloration doesn't stop after last letter)
In addition this widget would need to implement a working find and replace dialog.

Anyone interested???

edit: forgot to told you about the folding/unfolding abilities
have fun!

blockd
28th January 2006, 02:14
Anybody able to make a fast QTextEdit whose each line can be fully colored (I mean the background coloration doesn't stop after last letter)
I think you can do what you want with QTextBlockFormat::setBackground(const QBrush &).

fullmetalcoder
28th January 2006, 21:39
I already use the TextBlockFormat but I didn't find any way to make it color after the last character. Any hint/workaround will be welcomed :(

blockd
29th January 2006, 02:02
The following works for me. I only typed "a" as shown (no spaces or tabs), and that's worked for me on both X11 and (IIRC) Windows. Of course you'll still have to unhighlight the line when the current line changes, but this just shows it should be possible to do.

http://img81.imageshack.us/img81/7043/editor4my.png

editor.h


#ifndef EDITOR_H
#define EDITOR_H

#include <QTextEdit>

class Editor : public QTextEdit
{
Q_OBJECT

public:
explicit Editor(QWidget *parent = 0);
private slots:
void highlightCurrentLine();
};
#endif // EDITOR_H

editor.cpp


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

Editor::Editor(QWidget *parent):
QTextEdit(parent)
{
connect(this, SIGNAL(cursorPositionChanged()),
this, SLOT(highlightCurrentLine()));
}

void
Editor::highlightCurrentLine()
{
QTextCursor cursor = textCursor();
QTextBlock block = cursor.block();
QTextBlockFormat format = block.blockFormat();
format.setBackground(palette().alternateBase());
cursor.setBlockFormat(format);
}

main.cpp


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

int
main(int argc, char *argv[])
{
QApplication app(argc, argv);
Editor editor;
editor.show();
return app.exec();
}

fullmetalcoder
30th January 2006, 10:21
nice! Thanks for your help, I'll try it. :D

edit : :(

Tried it and it didn't work! Quite confusing!!! Which version of Qt are you using???
I already used that method to set the backgroud but with a QColor instead (which was actually converted to a QBrush implicitly...)

I think I'll start a thread on that subject in the Qt programming section, it may bring a solution...

blockd
1st February 2006, 01:19
Which version of Qt are you using???
Qt 4.1.0. If you're using a different version and it doesn't work, then I might consider that a bug or at least lack of documentation specifying the correct behaviour. But if you're using the same version, then that's pretty weird.

fullmetalcoder
1st February 2006, 09:39
I'm using a 4.1.1 snapshot but anyway I doubt it comes from that. Did you try your program under M$ Win??

blockd
1st February 2006, 14:48
I re-verified that it worked on Windows XP yesterday. Same version of Qt, 4.1.0.
I'll try the latest snapshot soon, since at this point it looks as if that's what the reason is.

fullmetalcoder
1st February 2006, 21:34
:eek: Thinking to something: do you use word wrap??? I think it's the default setting. My editor doesn't wrap anything so it may come from that!

I'll try it tomorrow!

edit : sorry posted it twice, damn buggy computer!!! And no way deleting posts!!!:rolleyes:

blockd
2nd February 2006, 01:32
Ah, wordwrap was the reason. I thought you'd tried the code I posted as is and it didn't work. Anyway, I'm about to post something that should work in the " Rich Text Tale Vol1 : Background colors" thread.