提供QTableWidget和QHeaderView来调节QTextTable的栏宽 写代码前感觉应该很简单。真正写的时候却遇到了不少问题,调试过程中也发现了很多写代码前没有考虑到的细节。以边学边写,不断调试,搞了好多天了。 现在回过来看,代码确实很简单。但有些细节还是没有处理到。如表格在文本的第一行时,此调节控件显示不出来。还有些槽函数可能要变为公用的。 mytableadjustor.h - #ifndef MYTABLEADJUSTOR_H
#define MYTABLEADJUSTOR_H
#include <QTableWidget> #include <QHeaderView> #include <QTextEdit> #include <QMouseEvent> #include <QTextCursor> #include <QTextTable> #include <QScrollBar>
#include <QDebug>
#define ADJUSTOR_HEIGHT 35
class MyTableAdjustor : public QTableWidget { Q_OBJECT public: explicit MyTableAdjustor(QTextEdit *parent = 0);
signals:
public slots:
private slots: void showTableAdjustor(); void adjustTableColumnWidth(); void repositionAdjustor();
protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void resizeEvent(QResizeEvent *event);
private: bool leftMouseButtonPressed; QTextEdit *parentTextEditor; QTextTable *currentTable; QTextTable *previousTable; };
#endif // MYTABLEADJUSTOR_H
mytableadjustor.cpp - #include "mytableadjustor.h"
MyTableAdjustor::MyTableAdjustor(QTextEdit *parent) : QTableWidget(0,0,parent) { setFixedHeight(ADJUSTOR_HEIGHT); setMinimumWidth(26); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVisible(false);
viewport()->setToolTip(tr("左右拖动鼠标可调整表格的整体大小")); viewport()->setCursor(Qt::SizeHorCursor);
horizontalHeader()->setCascadingSectionResizes(true); horizontalHeader()->setResizeMode(QHeaderView::Interactive);
parentTextEditor = parent; previousTable = 0;
connect(parentTextEditor,SIGNAL(cursorPositionChanged()),this,SLOT(showTableAdjustor())); connect(horizontalHeader(),SIGNAL(sectionResized(int,int,int)),this,SLOT(adjustTableColumnWidth())); connect(parentTextEditor->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(repositionAdjustor())); }
////////////////// protected function ////////////////////////////// void MyTableAdjustor::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) leftMouseButtonPressed = true; }
void MyTableAdjustor::mouseMoveEvent(QMouseEvent *event) { if (leftMouseButtonPressed) resize(event->x() - rect().left(), ADJUSTOR_HEIGHT); }
void MyTableAdjustor::resizeEvent(QResizeEvent *event) { int lastColumnWidthDiff = horizontalHeader()->length() - viewport()->width(); int lastColumnNumber = columnCount() - 1; int originalLastColumnWidth = horizontalHeader()->sectionSize(lastColumnNumber); horizontalHeader()->resizeSection(lastColumnNumber,originalLastColumnWidth - lastColumnWidthDiff); repositionAdjustor(); }
////////////////// private slots ////////////////////////////// void MyTableAdjustor::showTableAdjustor() { currentTable = parentTextEditor->textCursor().currentTable(); setVisible(currentTable);
if (currentTable && (currentTable != previousTable)){ QTextTableFormat tableFormat = currentTable->format(); int columnsCount = tableFormat.columns(); qreal borderWidth = tableFormat.border() * 2; int totalLength = 0; int sectionLength = 0; setColumnCount(columnsCount); for (int i = 0; i<columnsCount; i++){ sectionLength = (int)tableFormat.columnWidthConstraints().value(i).rawValue() + borderWidth; horizontalHeader()->resizeSection(i,sectionLength); totalLength += sectionLength; } resize(totalLength, ADJUSTOR_HEIGHT); repositionAdjustor(); } previousTable = currentTable; }
void MyTableAdjustor::adjustTableColumnWidth() { QTextTableFormat tableFormat = currentTable->format();
QVector<QTextLength> columnWidth; for(int i = 0; i < tableFormat.columns(); i++){ columnWidth << QTextLength(QTextLength::FixedLength, horizontalHeader()->sectionSize(i) - tableFormat.border() * 2); } tableFormat.setColumnWidthConstraints(columnWidth); currentTable->setFormat(tableFormat); }
void MyTableAdjustor::repositionAdjustor() { if (!isHidden()){ QPoint tablePosition(parentTextEditor->cursorRect(currentTable->firstCursorPosition()).topLeft()); qreal borderWidth = currentTable->format().border() * 2; move(tablePosition - QPoint(borderWidth,ADJUSTOR_HEIGHT + borderWidth)); } }
////////////////// private function //////////////////////////////
|