alexltr的个人主页

http://www.qtcn.org/bbs/u/107032  [收藏] [复制]

alexltr

我不是IT,只是喜欢Qt。 我不是程序员,只是与程序有缘。

  • 26

    关注

  • 60

    粉丝

  • 150

    访客

  • 等级:骑士
  • 身份:论坛版主
  • 总积分:537
  • 男,1976-01-01
  • 广州

最后登录:2024-04-20

更多资料

日志

动态调节QTextTable栏宽---MyTableAdjustor

2011-11-26 20:45
提供QTableWidget和QHeaderView来调节QTextTable的栏宽

写代码前感觉应该很简单。真正写的时候却遇到了不少问题,调试过程中也发现了很多写代码前没有考虑到的细节。以边学边写,不断调试,搞了好多天了。

现在回过来看,代码确实很简单。但有些细节还是没有处理到。如表格在文本的第一行时,此调节控件显示不出来。还有些槽函数可能要变为公用的。

mytableadjustor.h
  1. #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
  1. #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 //////////////////////////////


分类:默认分类|回复:0|浏览:2496|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:04-28 23:41
©2005-2016 QTCN开发网 版权所有 Gzip disabled