Hello everybody,
I have problem regarding QtextEdit and scrollView. My target is to make the cursor in TextEdit to be visible whenever I type something in it. The horizontal scrollBars should move accordingly once the cursor in the textEdit is at the end of the visible area of the frame. For this I have defined 3 classes namely
1) class myFrame : public QFrame
A frame which has a QtextEdit in it.
2) class myScroll : public QScrollView
ScrollView for scrolling purpose, which has myFrame in it. myFrame makes use of viewport defined in this class
3) class myWidget : public QMainWindow
A mainWidow which has scrollview
When I just type in the TextEdit, I want the scrollView to move the scrolls accordingly. This basically means I want the cursor in TextEdit to be visible anyTime I type in it.
I have written sample code but is unable to find what is wrong.
//test.h
#include <qscrollview.h>
#include <qframe.h>
#include <qtextedit.h>
#include <qmainwindow.h>
{
Q_OBJECT
public:
myFrame
( QWidget *parent
=0,
const char *name
="myFrame" );
~myFrame();
{
return myEditor;
}
public slots:
void slotTextChanged();
private:
};
class myScroll : public QScrollView
{
public:
myScroll
( QWidget *parent
=0,
const char *name
="ScrollChemCanvas" );
~myScroll();
private:
myFrame* f;
};
{
public:
myWidget
( QWidget *parent
=0,
const char *name
="myWidget" );
~myWidget();
private:
myScroll* scroll;
};
#include <qscrollview.h>
#include <qframe.h>
#include <qtextedit.h>
#include <qmainwindow.h>
class myFrame : public QFrame
{
Q_OBJECT
public:
myFrame ( QWidget *parent=0, const char *name="myFrame" );
~myFrame();
QTextEdit* getEditor()
{
return myEditor;
}
public slots:
void slotTextChanged();
private:
QTextEdit* myEditor;
};
class myScroll : public QScrollView
{
public:
myScroll ( QWidget *parent=0, const char *name="ScrollChemCanvas" );
~myScroll();
private:
myFrame* f;
};
class myWidget : public QMainWindow
{
public:
myWidget ( QWidget *parent=0, const char *name="myWidget" );
~myWidget();
private:
myScroll* scroll;
};
To copy to clipboard, switch view to plain text mode
//test.cpp
#include "test.h"
myWidget
::myWidget ( QWidget *parent,
const char *name
){
scroll = new myScroll(this);
CHECK_PTR( scroll );
scroll->setMinimumSize( 200, 200 );
setCentralWidget( scroll );
}
myWidget::~myWidget()
{
}
myScroll
::myScroll ( QWidget *parent,
const char *name
):QScrollView(parent, name)
{
f = new myFrame(viewport());
f->setBackgroundColor( Qt::blue );
f->resize( parentWidget()->width(), parentWidget()->height() );
f->getEditor()->resize( f->width(), f->getEditor()->height() );
addChild(f);
addChild(f->getEditor());
f->getEditor()->ensureCursorVisible();
}
myScroll::~myScroll()
{
}
myFrame
::myFrame ( QWidget *parent,
const char *name
){
connect( myEditor, SIGNAL( textChanged () ), this, SLOT(slotTextChanged()));
}
myFrame::~myFrame()
{
}
void myFrame::slotTextChanged()
{
}
#include "test.h"
myWidget::myWidget ( QWidget *parent, const char *name )
:QMainWindow(parent, name)
{
scroll = new myScroll(this);
CHECK_PTR( scroll );
scroll->setFrameStyle( QFrame::Box | QFrame::Raised );
scroll->setMinimumSize( 200, 200 );
setCentralWidget( scroll );
}
myWidget::~myWidget()
{
}
myScroll ::myScroll ( QWidget *parent, const char *name )
:QScrollView(parent, name)
{
f = new myFrame(viewport());
f->setBackgroundColor( Qt::blue );
f->resize( parentWidget()->width(), parentWidget()->height() );
f->getEditor()->resize( f->width(), f->getEditor()->height() );
addChild(f);
addChild(f->getEditor());
f->getEditor()->ensureCursorVisible();
}
myScroll::~myScroll()
{
}
myFrame::myFrame ( QWidget *parent, const char *name )
: QFrame(parent, name)
{
myEditor = new QTextEdit(parentWidget());
connect( myEditor, SIGNAL( textChanged () ), this, SLOT(slotTextChanged()));
}
myFrame::~myFrame()
{
}
void myFrame::slotTextChanged()
{
}
To copy to clipboard, switch view to plain text mode
//main.cpp
#include <qapplication.h>
#include "test.h"
int main( int argc, char ** argv )
{
myWidget w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
#include <qapplication.h>
#include "test.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
myWidget w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Help me to fix this problem.
Thanks in advance
Bookmarks