找回密码
 立即注册
收起左侧

关于制作类似于信息框的问题

2
回复
7928
查看
[复制链接]
累计签到:28 天
连续签到:1 天
来源: 2018-5-7 14:39:18 显示全部楼层 |阅读模式
10Qter豆
在网上找了很久没找到相关资料,想了很久也没有头绪,上个月才开始学习QT,很多地方都不明白,困扰了一周了,可否提供类似的源码,我想要学习一下,谢谢各位.

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册

最佳答案

查看完整内容

一个例子 logbox.h logbox.cpp
回复

使用道具 举报

累计签到:436 天
连续签到:1 天
2018-5-7 14:39:19 显示全部楼层
本帖最后由 miroox 于 2018-5-7 19:56 编辑

一个例子

logbox.h

  1. #ifndef LOGBOX_H
  2. #define LOGBOX_H

  3. #include <QWidget>

  4. class QTextEdit;

  5. class LogBox : public QWidget
  6. {
  7.     Q_OBJECT

  8. public:
  9.     LogBox(QWidget *parent = 0);
  10.     ~LogBox();

  11. public slots:
  12.     void append(const QString& str);

  13. private slots:
  14.     void highlightCurrentLine();

  15. private:
  16.     QTextEdit* edit;
  17. };

  18. #endif // LOGBOX_H
复制代码


logbox.cpp

  1. #include "logbox.h"
  2. #include <QTextEdit>
  3. #include <QHBoxLayout>

  4. LogBox::LogBox(QWidget *parent)
  5.     : QWidget(parent)
  6. {
  7.     QHBoxLayout* lay = new QHBoxLayout(this);
  8.     edit = new QTextEdit;
  9.     lay->addWidget(edit);
  10.     edit->setReadOnly(true);
  11.     connect(edit,&QTextEdit::cursorPositionChanged,
  12.             this,&LogBox::highlightCurrentLine);
  13. }

  14. LogBox::~LogBox()
  15. {
  16. }

  17. void LogBox::append(const QString& str)
  18. {
  19.     edit->append(str);
  20. }

  21. void LogBox::highlightCurrentLine()
  22. {
  23.     QList<QTextEdit::ExtraSelection> extraSelections;

  24.     QTextCursor cur = edit->textCursor();
  25.     cur.movePosition(QTextCursor::StartOfBlock);
  26.     cur.clearSelection();
  27.     int curBlock = cur.blockNumber();
  28.     for (bool succ=true;
  29.          cur.blockNumber()==curBlock && succ;
  30.          succ=cur.movePosition(QTextCursor::Down))
  31.     {
  32.         QTextEdit::ExtraSelection selection;

  33.         selection.format.setBackground(QColor(Qt::blue));
  34.         selection.format.setForeground(QColor(Qt::white));
  35.         selection.format.setProperty(QTextFormat::FullWidthSelection, true);

  36.         selection.cursor = cur;
  37.         //selection.cursor.clearSelection();
  38.         extraSelections.append(selection);
  39.     }

  40.     edit->setExtraSelections(extraSelections);
  41. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

累计签到:28 天
连续签到:1 天
2018-5-8 22:22:25 显示全部楼层
miroox 发表于 2018-5-7 14:39
一个例子

logbox.h

非常感谢,很有帮助                                                                                                                                                                                    
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了