Qt - several QTextBlock inline

谁都会走 提交于 2019-12-07 05:32:06

问题


Is it possible to arrange several QTextBlocks in QTextDocument in one horizontal line?

I need to know which block of text was clicked and QTextBlock would be nice to use due to its method setUserState(int), which can be used to hold id of particular block. Are there better approaches?


回答1:


Not sure if I got your question right, but I am taking a shot at it (some three years after the question was asked.....)

In principle you can put QTextBlocks in a horizontal line using a QTextTable. If you then create a class which inherits from QTextEdit you can catch the mouse events and find out which text block was clicked.

I post some code below where I have a very simply dialog that only has a textedit in it (of the derived class mentioned above). I create a table laying out three text blocks in a horizontal line and set their user state to the column number. Then I have the text edit class only with the overloaded mouseEvent method which only prints the userState of whatever text block it is in to the command line, just to show the principle.

Let me know if this is of any help or if misunderstood your question.

dialog.h

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include "ui_dialog.h"

class MyDialog : public QDialog, public Ui::Dialog
{
 public:
  MyDialog(QWidget * parent = 0, Qt::WindowFlags f = 0);
  void createTable();

};


#endif

dialog.cpp

#include "dialog.h"

#include <QTextTable>
#include <QTextTableFormat>

MyDialog::MyDialog(QWidget * parent, Qt::WindowFlags f) :
QDialog(parent,f)
{
  setupUi(this);
}

void MyDialog::createTable()
{
  QTextCursor cursor = textEdit->textCursor();

  QTextTableFormat tableFormat;
  tableFormat.setCellPadding(40);
  tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None);

  QTextTable* table=cursor.insertTable(1,3,tableFormat);
  for( int col = 0; col < table->columns(); ++col ) {
    cursor = table->cellAt(0, col).firstCursorPosition();
    cursor.insertBlock();
    cursor.block().setUserState(col);
    cursor.insertText(QString("Block in Column ")+QString::number(col));
  }

}

mytextedit.h

#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H

#include <QTextEdit>

class MyTextEdit : public QTextEdit
{
 public:
  MyTextEdit(QWidget * parent = 0);
  void mousePressEvent(QMouseEvent *event);
};
#endif

mytextedit.cpp

#include "mytextedit.h"

#include <QMouseEvent>
#include <QTextBlock>
#include <QtCore>

MyTextEdit::MyTextEdit(QWidget * parent) :
  QTextEdit(parent)
{

}

void MyTextEdit::mousePressEvent(QMouseEvent *event)
{
  if(event->button() == Qt::LeftButton) {
    qDebug() << this->cursorForPosition(event->pos()).block().userState();
  }
}

main.cpp (just for completeness)

#include <QApplication>
#include "dialog.h"

int main(int argc, char** argv)
{
  QApplication app(argc,argv);
  MyDialog dialog;

  dialog.show();
  dialog.createTable();

  return app.exec();

}


来源:https://stackoverflow.com/questions/12041341/qt-several-qtextblock-inline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!