Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - several QTextBlock inline

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?

like image 894
Dejwi Avatar asked Nov 14 '22 00:11

Dejwi


1 Answers

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();

}
like image 159
Erik Avatar answered Dec 15 '22 23:12

Erik