• 17159阅读
  • 9回复

QTextEdit类实现内置滚动条时只能竖直滚动不能水平滚动 [复制链接]

上一主题 下一主题
离线云起时分
 
只看楼主 倒序阅读 楼主  发表于: 2011-04-04
关键词: QTCreatorC++
Qt Creator中用QTextEdit类实现内置滚动条时,在文本区域一直按回车键可以出现竖直滚动条,但是添加文本后只会换行而不能实现水平滚动?我的C++源代码如下:
  1. #include<qapplication.h>
  2. #include<qpushbutton.h>
  3. #include<qfont.h>
  4. #include<qwidget.h>
  5. #include<qscrollarea.h>
  6. #include<qtextedit.h>
  7. //we base our new class on QScrollArea instead of QWidget:
  8. class MyMainWindow:public QScrollArea
  9. {
  10. public:
  11.     MyMainWindow();
  12. private:
  13.     //we create a QWidget Object as a child widget to the QScrollArea Object instead:
  14.     QWidget *main;
  15.     QPushButton *b1;
  16.     QPushButton *b2;
  17.     QPushButton *b3;
  18.     QPushButton *b4;
  19.     QPushButton *b5;
  20.     QTextEdit *edit1;
  21. };
  22. MyMainWindow::MyMainWindow()
  23. {
  24.     //set the geometry for the scrollarea:
  25.     setGeometry(100,100,470,410);
  26.     main=new QWidget(this);
  27.     main->resize(460,400);
  28.     b1=new QPushButton("New",main);
  29.     b1->setGeometry(10,10,80,30);
  30.     b1->setFont(QFont("Times",18,QFont::Bold));
  31.     b2=new QPushButton("Open",main);
  32.     b2->setGeometry(100,10,80,30);
  33.     b2->setFont(QFont("Times",18,QFont::Bold));
  34.     b3=new QPushButton("Save",main);
  35.     b3->setGeometry(190,10,80,30);
  36.     b3->setFont(QFont("Times",18,QFont::Bold));
  37.     b4=new QPushButton("Print",main);
  38.     b4->setGeometry(280,10,80,30);
  39.     b4->setFont(QFont("Times",18,QFont::Bold));
  40.     b5=new QPushButton("Quit",main);
  41.     b5->setGeometry(370,10,80,30);
  42.     b5->setFont(QFont("Times",18,QFont::Bold));
  43.     edit1=new QTextEdit(main);
  44.     edit1->setGeometry(0,50,440,340);
  45.     edit1->setText("Let's pretend this is a text editor.");
  46.     //we must call the QScrollArea::setWidget function for each of its
  47.     //child widgets. Since main holds all our other widgets,we only
  48.     //have to insert that one:
  49.     setWidget(main);
  50. }
  51. void main(int argc,char **argv)
  52. {
  53.     QApplication a(argc,argv);
  54.     MyMainWindow w;
  55.     a.setActiveWindow(&w);
  56.     w.show();
  57.     a.exec();
  58. }

初学,还请大家能帮忙指教!非常感谢!
离线wxj120bw

只看该作者 1楼 发表于: 2011-04-04
引用楼主云起时分于2011-04-04 14:28发表的 QTextEdit类实现内置滚动条时只能竖直滚动不能水平滚动 :
Qt Creator中用QTextEdit类实现内置滚动条时,在文本区域一直按回车键可以出现竖直滚动条,但是添加文本后只会换行而不能实现水平滚动?我的C++源代码如下:
[code]
#include<qapplication.h>
#include<qpushbutton.h>
#include<qfont.h>
.......

你需要判断是否要水平移动,如果要,就发送水平移动数字的信号,这样就可以实现水平移动。可能还有其他的方式实现。
离线云起时分
只看该作者 2楼 发表于: 2011-04-04
你好!多谢你的回答!还想占用一下大侠的宝贵时间,请问我该怎样判断是否要水平移动呢?
我是参考的书上的程序,上面并没有提到这些,只说只要在文本区域添加文本,当文本行太长时滚动条就会显示出来。
离线wxj120bw

只看该作者 3楼 发表于: 2011-04-04
引用第2楼云起时分于2011-04-04 19:07发表的  :
你好!多谢你的回答!还想占用一下大侠的宝贵时间,请问我该怎样判断是否要水平移动呢?
我是参考的书上的程序,上面并没有提到这些,只说只要在文本区域添加文本,当文本行太长时滚动条就会显示出来。

QTextEdit控件默认是不会水平滚动的 调用 setHorizontalScrollBarPolicy  函数,用于制定控件的水平移动方式。
离线云起时分
只看该作者 4楼 发表于: 2011-04-04
好的,非常感谢大侠的指点!我再试试!
离线云起时分
只看该作者 5楼 发表于: 2011-04-04
你好!我在网上搜了资料看过,资料不多,多数都是讲的都是水平滚动条的隐藏和显现,我用edit1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);只能让它显现出来,而且是灰色的。无论我怎么添加文本,水平滚动条都不起作用!我是不是还要设置什么?才能在当输入的文本行过长时水平滚动条起作用啊?
我的设置如下:
QTextEdit *edit1;

edit1=new QTextEdit(main);
    edit1->setGeometry(10,50,430,340);
    edit1->setText("Let's pretend this is a text editor.");
    edit1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

烦请大侠再为指点一下,非常感谢!
离线云起时分
只看该作者 6楼 发表于: 2011-04-04
还想请教一下大侠,可以阅读哪些资料来了解这些函数的功能以及参数含义和设置?用到这些函数的时候,看到那些参数,觉得很迷茫,是不是必须得看Qt的Assistant才能了解各个函数?
离线wxj120bw

只看该作者 7楼 发表于: 2011-04-06
你查看QTextEdit的这两个属性

lineWrapColumnOrWidth : int
lineWrapMode
: LineWrapMode

可以实现水平滚动

遇到这种问题,的确需要查看qt assistant帮助,遇到qt控件的问题,先去看下类的properties,或者控件的介绍,简单的说,就是多操作,慢慢会理解的。(回复进高级模式,要不我需要从我的回复中找你的帖子)
离线云起时分
只看该作者 8楼 发表于: 2011-04-06
非常感谢你的指导,我已经可以实现水平内置滚动条的滚动了!qt assistant真是个好东东。我参考qt assistant中的方法,用QTextEdit::setLineWrapMode(LineWrapMode mode)设置了它的滚动模式为setLineWrapColumnOrWidth,接着用QTextEdit::setLineWrapColumnOrWidth(int w)设置了水平宽度,只要宽度设置得比文本区域宽,一旦文本行长度超出了文本域的宽度,就可以实现水平滚动了。(在此说这么多,一是当是自己做的笔记,二来也给和我一样的新手们提供些参考,愿他们不用费太多的时间!)
离线云起时分
只看该作者 9楼 发表于: 2011-04-06
笔记:上面的设置只能针对输入的文本为连续无间断的情况。
如果输入的文本有间隔时,可以用QTextEdit::setLineWrapMode(LineWrapMode mode)设置它的滚动模式为QTextEdit::FixedPixelWidth,其它的设置同以上的方法,即可实现有间隔文本行的水平滚动。
快速回复
限100 字节
 
上一个 下一个