簡體   English   中英

彈出窗口顯示從標題項右上角開始的矩形中的垂直標題文本

[英]popup showing vertical header text in rectangle starting top right corner of header item

我嘗試制作顯示垂直標題項目名稱的彈出標簽,因為名稱很大並且我希望標題寬度只是其編號的大小。
我制作了事件過濾器並制作了彈出對話框和 popuplabel qlabel。
但彈出窗口的大小大於標題項的大小。
如果我使 size 等於 rect size ,則文本消失。
如果我調整彈出窗口的大小,它會顯示更大的矩形顯示文本,向下偏移對應於標題項的視覺索引。
這是代碼:

#ifndef TABLEVIEW_H
#define TABLEVIEW_H

#include <QDialog>
#include <QEvent>
#include <QLabel>
#include <QMouseEvent>
#include <QTableView>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QTimer>

class TableView : public QTableView {
    Q_OBJECT
        QDialog* popup;
    QLabel* popupLabel;
    int editor_index ;

public:

    TableView(QWidget* parent = Q_NULLPTR) :QTableView(parent) {
        viewport()->installEventFilter(this);
        horizontalHeader()->viewport()->installEventFilter(this);
        verticalHeader()->viewport()->installEventFilter(this);
        setMouseTracking(true);
        popup = new QDialog(this, Qt::Popup | Qt::ToolTip);
        //popup = new QDialog(viewport(), Qt::Popup | Qt::ToolTip);

        //QVBoxLayout* layout = new QVBoxLayout;
        popupLabel = new QLabel(popup);
        popupLabel->setWordWrap(false);
        //layout->addWidget(popupLabel);
        popupLabel->setTextFormat(Qt::RichText);        
        //popup->setLayout(layout);
        popup->installEventFilter(this);
        QTimer::singleShot(0, popup, &QWidget::hide);

    }

    bool eventFilter(QObject* watched, QEvent* event) {
        //if (viewport() == watched) 
        if ((watched == horizontalHeader()->viewport() ||
            watched == verticalHeader()->viewport())) 
        {
            if (event->type() == QEvent::MouseMove) 
            {
                /*/
                if (popup) { //delete previous popup just in case
                    popup->contentsRect().setRect(0,0,0,0) ;
                }
                if (popupLabel) { //delete previous popupLabel just in case
                    popupLabel->contentsRect().setRect(0,0,0,0);
                }
                */
                /*
                ////////////////////////////////////////////////////////
                popup = new QDialog(this, Qt::Popup | Qt::ToolTip);
                //popup = new QDialog(viewport(), Qt::Popup | Qt::ToolTip);

                //QVBoxLayout* layout = new QVBoxLayout;
                popupLabel = new QLabel(popup);
                popupLabel->setWordWrap(false);
                //layout->addWidget(popupLabel);
                popupLabel->setTextFormat(Qt::RichText);
                //popup->setLayout(layout);
                popup->installEventFilter(this);
                QTimer::singleShot(0, popup, &QWidget::hide);
                ////////////////////////////////////////////////////////
                */

                QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);                
                QHeaderView* header = static_cast<QHeaderView*>(watched->parent());
                int mouse_pos = header->orientation() == Qt::Horizontal ? mouseEvent->x() : mouseEvent->y();
                int logical_index = header->logicalIndex(header->visualIndexAt(mouse_pos));
                                
                if (logical_index >= 0) 
                { // if mouse is over an item               
                    showPopup(logical_index, watched);                    
                }                
                else 
                {
                    popup->hide();
                }
            }
            else if (event->type() == QEvent::Leave) {
                popup->hide();
            }
        }
        else if (popup == watched) 
        {
            if (event->type() == QEvent::Leave) 
            {
                popup->hide();
            }
        }
        return QTableView::eventFilter(watched, event);
    }

private:
    void showPopup(const int & logical_index, QObject* watched) const 
    {
        if (logical_index >= 0)
        {
        QHeaderView* header = static_cast<QHeaderView*>(watched->parent());

        QFont font("times", 12);
        QFontMetrics fm(font);
        int pixelsWide = fm.width("xxxxxx");
        int pixelsHigh = fm.height();

        QRect rect; // line edit rect in header's viewport's coordinates
        if (header->orientation() == Qt::Horizontal) {
            //rect.setLeft(header->sectionPosition(logical_index));
            rect.setLeft(header->sectionViewportPosition(logical_index));
            rect.setWidth(header->sectionSize(logical_index));
            rect.setTop(0);
            rect.setHeight(header->height());
        }
        else {
            //rect.setTop((header->sectionPosition(logical_index)));
            rect.setTop((header->sectionViewportPosition(logical_index)));
            //QPoint point(0,header->sectionPosition(logical_index));
            //rect.setTop((mapToGlobal(point)).y() );

            //rect.topLeft())
            int cy = header->sectionSize(logical_index);
            rect.setHeight(header->sectionSize(logical_index));
            rect.setLeft(0);
            rect.setWidth(header->width());
            //rect.setCoords(rect.left(),rect.top(),rect.left()+rect.width(),rect.top()+rect.height());
        }
        
        //rect.adjust(1, 1, 1, 1);        
        popupLabel->move(rect.bottomLeft());
        //popupLabel->move(rect.topLeft());
        //popupLabel->move(viewport()->mapToGlobal(rect.topLeft()));

        popupLabel->resize(rect.size());
        //popupLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
        //popupLabel->show();

        //popupLabel->setFixedHeight(rect.height());
        //popupLabel->setFixedWidth(rect.width());
        //popupLabel->setFrame(false);
        //get current item text
        QString text = header->model()->
            headerData(logical_index, header->orientation()).toString();
        //int z=text.size();
        popupLabel->setText(text);
        //popupLabel->setFocus();
        ///////////////////////////////////////////////////////////////////////////
        //editor_index = logical_index; //save for future use
        popupLabel->installEventFilter(this->parent()); //catch focus out event
        //if user presses Enter it should close editor
        //connect(header_editor, SIGNAL(returnPressed()),
        //    ui->tableWidget, SLOT(setFocus()));
        //popupLabel->setGeometry(rect);
        //popupLabel->adjustSize();
        //popupLabel->setFixedSize(100, 100);
        popupLabel->show();
        ////////////////////////////////////////////////////////////////////////////
        //popup->move(viewport()->mapToGlobal(rect.bottomLeft()));
        popup->move(viewport()->mapToGlobal(rect.topLeft()));
        //popup->move(rect.topLeft());
        //popup->resize(rect.size());

        //popup->move(rect.bottomLeft());
        //popup->move(rect.topLeft());

        //popup->setFixedSize(100, popup->heightForWidth(100));
        //popup->setFixedSize(100, 100);

        //popupLabel->setText(text);
        //popupLabel->show();

        // popupLabel->setText(logical_index.data(Qt::DisplayRole).toString());
        //popup->adjustSize();
        //popup->setGeometry(rect);
        popup->show();
        //sleep 10 ms;
        return;// true; // filter out event
        }
        else {
            popup->hide();
             //delete previous popup just in case
            popup->contentsRect().setRect(0, 0, 0, 0);
            //popup = nullptr;
            
             //delete previous popupLabel just in case
            popupLabel->contentsRect().setRect(0, 0, 0, 0);
            //popupLabel = nullptr;
            
            return; //false;
        }
    }
};

#endif // TABLEVIEW_H

我使用超越比較來找出我改變了什么:

1.我將popupLabel移動到點0,0

2.我將popup調整為rect.size()

如果不是顯示文本的popupLabel空間,我將調整popupLabelpopup到更大的大小。

代碼:

#include <QApplication>
#include <QTableWidget>
#include <QStyledItemDelegate>
#include <QDialog>
#include <QEvent>
#include <QLabel>
#include <QMouseEvent>
#include <QTableView>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QTimer>
#include <QDebug>

class TableModel :public QAbstractTableModel
{
    Q_OBJECT
public:
    int rowCount(const QModelIndex& parent = QModelIndex()) const
    {
        return 4;
    }
    int columnCount(const QModelIndex& parent = QModelIndex()) const
    {
        return 2;
    }
    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
    {
        if (role == Qt::DisplayRole)
            return QString::number(index.row()) + QString::number(index.column());
        return QVariant();
    }
};

class TableView : public QTableView {
    Q_OBJECT
        QDialog* popup;
    QLabel* popupLabel;
    int editor_index;

public:

    TableView(QWidget* parent = Q_NULLPTR) :QTableView(parent) {
        viewport()->installEventFilter(this);
        horizontalHeader()->viewport()->installEventFilter(this);
        verticalHeader()->viewport()->installEventFilter(this);
        setMouseTracking(true);
        popup = new QDialog(this, Qt::Popup | Qt::ToolTip);
        //popup = new QDialog(viewport(), Qt::Popup | Qt::ToolTip);

        //QVBoxLayout* layout = new QVBoxLayout;
        popupLabel = new QLabel(popup);
        popupLabel->setWordWrap(false);
        //layout->addWidget(popupLabel);
        popupLabel->setTextFormat(Qt::RichText);
        //popup->setLayout(layout);
        popup->installEventFilter(this);
        QTimer::singleShot(0, popup, &QWidget::hide);

    }

    bool eventFilter(QObject* watched, QEvent* event) {
        //if (viewport() == watched) 
        if ((watched == horizontalHeader()->viewport() ||
            watched == verticalHeader()->viewport()))
        {
            if (event->type() == QEvent::MouseMove)
            {
                /*/
                if (popup) { //delete previous popup just in case
                    popup->contentsRect().setRect(0,0,0,0) ;
                }
                if (popupLabel) { //delete previous popupLabel just in case
                    popupLabel->contentsRect().setRect(0,0,0,0);
                }
                */
                /*
                ////////////////////////////////////////////////////////
                popup = new QDialog(this, Qt::Popup | Qt::ToolTip);
                //popup = new QDialog(viewport(), Qt::Popup | Qt::ToolTip);

                //QVBoxLayout* layout = new QVBoxLayout;
                popupLabel = new QLabel(popup);
                popupLabel->setWordWrap(false);
                //layout->addWidget(popupLabel);
                popupLabel->setTextFormat(Qt::RichText);
                //popup->setLayout(layout);
                popup->installEventFilter(this);
                QTimer::singleShot(0, popup, &QWidget::hide);
                ////////////////////////////////////////////////////////
                */

                QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
                QHeaderView* header = static_cast<QHeaderView*>(watched->parent());
                int mouse_pos = header->orientation() == Qt::Horizontal ? mouseEvent->x() : mouseEvent->y();
                int logical_index = header->logicalIndex(header->visualIndexAt(mouse_pos));

                if (logical_index >= 0)
                { // if mouse is over an item               
                    showPopup(logical_index, watched);
                }
                else
                {
                    popup->hide();
                }
            }
            else if (event->type() == QEvent::Leave) {
                popup->hide();
            }
        }
        else if (popup == watched)
        {
            if (event->type() == QEvent::Leave)
            {
                popup->hide();
            }
        }
        return QTableView::eventFilter(watched, event);
    }

private:
    void showPopup(const int& logical_index, QObject* watched) const
    {
        if (logical_index >= 0)
        {
            QHeaderView* header = static_cast<QHeaderView*>(watched->parent());

            QFont font("times", 12);
            QFontMetrics fm(font);
            int pixelsWide = fm.width("xxxxxx");
            int pixelsHigh = fm.height();

            QRect rect; // line edit rect in header's viewport's coordinates
            if (header->orientation() == Qt::Horizontal) {
                //rect.setLeft(header->sectionPosition(logical_index));
                rect.setLeft(header->sectionViewportPosition(logical_index));
                rect.setWidth(header->sectionSize(logical_index));
                rect.setTop(0);
                rect.setHeight(header->height());
            }
            else {
                //rect.setTop((header->sectionPosition(logical_index)));
                rect.setTop((header->sectionViewportPosition(logical_index)));
                //QPoint point(0,header->sectionPosition(logical_index));
                //rect.setTop((mapToGlobal(point)).y() );

                //rect.topLeft())
                int cy = header->sectionSize(logical_index);
                rect.setHeight(header->sectionSize(logical_index));
                rect.setLeft(0);
                rect.setWidth(header->width());
                //rect.setCoords(rect.left(),rect.top(),rect.left()+rect.width(),rect.top()+rect.height());
            }

            //rect.adjust(1, 1, 1, 1);        
            //popupLabel->move(rect.bottomLeft());
            //popupLabel->move(rect.topLeft());
            popupLabel->move(QPoint(0,0));
            popupLabel->resize(rect.size());
            //popupLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
            //popupLabel->show();

            //popupLabel->setFixedHeight(rect.height());
            //popupLabel->setFixedWidth(rect.width());
            //popupLabel->setFrame(false);
            //get current item text
            QString text = header->model()->
                headerData(logical_index, header->orientation()).toString();
            //int z=text.size();
            popupLabel->setText(text);  
            qDebug() << rect << text;
            //popupLabel->setFocus();
            ///////////////////////////////////////////////////////////////////////////
            //editor_index = logical_index; //save for future use
            popupLabel->installEventFilter(this->parent()); //catch focus out event
            //if user presses Enter it should close editor
            //connect(header_editor, SIGNAL(returnPressed()),
            //    ui->tableWidget, SLOT(setFocus()));
            //popupLabel->setGeometry(rect);
            //popupLabel->adjustSize();
            //popupLabel->setFixedSize(100, 100);
            popupLabel->show();
            ////////////////////////////////////////////////////////////////////////////
            //popup->move(viewport()->mapToGlobal(rect.bottomLeft()));
            popup->move(viewport()->mapToGlobal(rect.topLeft()));
            //popup->move(rect.topLeft());
            popup->resize(rect.size());

            //popup->move(rect.bottomLeft());
            //popup->move(rect.topLeft());

            //popup->setFixedSize(100, popup->heightForWidth(100));
            //popup->setFixedSize(100, 100);

            //popupLabel->setText(text);
            //popupLabel->show();

            // popupLabel->setText(logical_index.data(Qt::DisplayRole).toString());
            //popup->adjustSize();
            //popup->setGeometry(rect);
            popup->show();
            //sleep 10 ms;
            return;// true; // filter out event
        }
        else {
            popup->hide();
            //delete previous popup just in case
            popup->contentsRect().setRect(0, 0, 0, 0);
            //popup = nullptr;

             //delete previous popupLabel just in case
            popupLabel->contentsRect().setRect(0, 0, 0, 0);
            //popupLabel = nullptr;

            return; //false;
        }
    }
};
#include"main.moc"
int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    TableView view;
    view.setModel(new TableModel);
    view.show();
    return a.exec();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM