QTextDocument の複数の QTextBlocks を 1 つの水平線に配置することは可能ですか?
どのテキストブロックがクリックされたかを知る必要があり、特定のブロックの ID を保持するために使用できるメソッド setUserState(int) があるため、QTextBlock を使用すると便利です。より良いアプローチはありますか?
QTextDocument の複数の QTextBlocks を 1 つの水平線に配置することは可能ですか?
どのテキストブロックがクリックされたかを知る必要があり、特定のブロックの ID を保持するために使用できるメソッド setUserState(int) があるため、QTextBlock を使用すると便利です。より良いアプローチはありますか?
あなたの質問が正しかったかどうかはわかりませんが、私はそれを試しています(質問が出されてから約3年後.....)
QTextBlocks
原則として、 を使用して横線を入れることができますQTextTable
。その後、継承するクラスを作成するQTextEdit
と、マウス イベントをキャッチして、クリックされたテキスト ブロックを見つけることができます。
(上記の派生クラスの) textedit のみを含む非常に単純なダイアログがあるコードを以下に投稿します。3 つのテキスト ブロックを横一列に並べた表を作成し、それらのユーザー状態を列番号に設定します。次に、原則を示すために、テキストブロックの内容をコマンドラインにmouseEvent
出力するだけのオーバーロードされたメソッドを使用して、テキスト編集クラスを作成します。userState
これが役立つかどうか、または質問を誤解している場合はお知らせください。
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
ダイアログ.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 (完全を期すため)
#include <QApplication>
#include "dialog.h"
int main(int argc, char** argv)
{
QApplication app(argc,argv);
MyDialog dialog;
dialog.show();
dialog.createTable();
return app.exec();
}