RameshNani
30th January 2015, 04:39
Hi , In my custom Header i am painting the sections .
for the frozen column also we are getting header column , but this header column override with customheaderview column , so it's moving , is there any way to prevent the painting perticular section in QHeaderView . when i pass same customheader to frozen table widget and when i use this one for frozentableview the maintableview columns also hiding . so other than this i created new custom header object and given for frozen tableview also but i have same issue . i need it very urgently . could any one help me .
Hi Could you please review my code
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <MySortfilterProxyModel.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MySortFilterProxyModel * proxy;
public slots :
void setFilterColumn(int);
void setFilterRegExpxx(int,QString);
void setFilterRegExpU(int,QString);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "freezetablewidget.h"
#include <QtGui>
#include "QSqlDatabase"
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <MySortfilterProxyModel.h>
#include <CustomHeader.h>
#include <QDir>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDir *dir = new QDir();
qDebug()<<"Current Path :"<<dir->currentPath();
//QDir *dir = new QDir();
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//QDir *dir = new QDir();
// db.setDatabaseName("F:/sqlite/test.db");
//QDir *dir = new QDir();
db.setDatabaseName(dir->currentPath()+"/DataBase/test.db");
db.open();
QSqlQueryModel *model = new QSqlQueryModel();
// QSqlTableModel *model = new QSqlTableModel(this,db);
// QAbstractTableModel *model = new QAbstractTableModel();
QSqlQueryModel *modelsum = new QSqlQueryModel();
proxy = new MySortFilterProxyModel();
proxy->setSourceModel(model);
CustomHeader *m_header = new CustomHeader(Qt::Horizontal);
FreezeTableWidget *tableView = new FreezeTableWidget(proxy,m_header);
tableView->setHorizontalHeader(m_header);
// tableView->horizontalHeader()->hide();
// tableView->setColumnHidden(0, true);
CustomHeader *sumHeader = new CustomHeader(Qt::Horizontal);
FreezeTableWidget *tableViewsum = new FreezeTableWidget(modelsum,sumHeader);
tableViewsum->horizontalHeader()->hide();
tableViewsum->horizantalHeader_Hide();
connect(m_header, SIGNAL(searchString(int,QString)), this, SLOT(setFilterRegExpxx(int,QString)));
connect(m_header, SIGNAL(indexOfColumn(int)), this, SLOT(setFilterColumn(int)));
//SUPRAJA JAN 20
connect(m_header, SIGNAL(unfilter(int,QString)), this, SLOT(setFilterRegExpU(int,QString)));
/*
QHeaderView *m_header = new CustomHeader(Qt::Horizontal);
childView->setHorizontalHeader(m_header);
connect(m_header, SIGNAL(searchString(int,QString)), this, SLOT(setFilterRegExpxx(int,QString)));
connect(m_header, SIGNAL(indexOfColumn(int)), this, SLOT(setFilterColumn(int)));
//SUPRAJA JAN 20
connect(m_header, SIGNAL(unfilter(int,QString)), this, SLOT(setFilterRegExpU(int,QString)));
*/
model->setQuery("select symbol,security,quantity,symbol as M_Symbol,security as M_Security,quantity as M_Quantity,symbol as R_Symbol,security as R_Security,quantity as R_Quantity from portfolio",db);
// QSqlQuery query="select symbol,security,quantity,symbol as M_Symbol,security as M_Security,quantity as M_Quantity,symbol as R_Symbol,security as R_Security,quantity as R_Quantity from portfolio";
// model->setQuery(query);
modelsum->setQuery("select symbol,security,quantity,symbol as M_Symbol,security as M_Security,quantity as M_Quantity,symbol as R_Symbol,security as R_Security,quantity as R_Quantity from portfolio",db);
//
// connect(tableViewsum,SIGNAL(tableViewsum->horizontalHeader()->scroll()),tableView,);
connect(tableViewsum->horizontalScrollBar(), SIGNAL(valueChanged(int)),tableView->horizontalScrollBar(), SLOT(setValue(int)));
tableView->setModel(proxy);
// tableView->setColumnHidden(0, true);
tableViewsum->setModel(modelsum);
QWidget *centralwidget = new QWidget(this);
this->setCentralWidget( centralwidget );
QVBoxLayout *layout = new QVBoxLayout(centralwidget);
layout->addWidget(tableView);
layout->addWidget(tableViewsum);
centralwidget->setLayout(layout);
// layout->
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setFilterRegExpxx(int index,QString ss)
{
qDebug()<<"setfilter reg expression";
// if(proxy->multifilter.contains(index))
// proxy->multifilter[index]=ss;
// else
// proxy->multifilter.insert(index,ss);
std::string sss=ss.toStdString();
proxy->multifilter[index]=sss;
proxy->setFilterRegExp(ss);
}
void MainWindow::setFilterRegExpU(int index,QString ss)
{
//if(proxy->multifilter.find(index)!=-1)
{
// proxy->multifilter.remove(index);
qDebug()<<"Erase From Map :"<<index;
proxy->multifilter.erase(index);
proxy->setFilterRegExp(ss);
}
}
void MainWindow::setFilterColumn(int i)
{
qDebug()<<"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS SS SetFilter Key column";
proxy->setFilterKeyColumn(i);
// proxyModel->sort(i);
}
Added after 4 minutes:
CustomHeader.h
#ifndef CUSTOMHEADER_H
#define CUSTOMHEADER_H
#include <QHeaderView>
#include <QtGui>
//#include "HideColumnsCombo.h"
class CustomHeader : public QHeaderView
{
Q_OBJECT
public:
CustomHeader(Qt::Orientation orientation, QWidget * parent = 0);
bool sflag,icon;
QVector<int> filterIndex;
void testfun();
protected:
void mousePressEvent(QMouseEvent * event);
void mouseMoveEvent(QMouseEvent * event);
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
QRect sectionRect(int logicalIndex) const;
QRect buttonMenuRect(int logicalIndex) const;
QRect prevRect(int logicalIndex) const;
QRect nextRect(int logicalIndex) const;
void drawMenuButton(QPainter *painter, int logicalIndex, bool enabled) const;
void drawMenuButton1(QPainter *painter, int logicalIndex, bool enabled) const;
void drawPrevButton(QPainter *painter, int logicalIndex) const;
void drawNextButton(QPainter *painter, int logicalIndex) const;
public slots:
void changedData(QString);
//void unfilter(QString);
void copy1();
void slot_changed(const QModelIndex& , const QModelIndex& );
void UnfilterColumn() ;
signals:
void searchString(int,QString);
void unfilter(int,QString);
void indexOfColumn(int);
private:
mutable bool mouseoverflg;
//HideColumnsCombo *list;
QComboBox *comboBox;
QStandardItemModel *Model;
QStandardItem *Item1;
QVector<QStandardItem*> Items;
QMap<QString,int> m_hideColumns;
QWidgetAction *hideAction;
};
#endif
CustomHeader.cpp
#include <QPainter>
#include <QCursor>
#include <QHoverEvent>
#include <QMenu>
//nclude "CheckBoxList.h"
#include "CustomHeader.h"
int disableflag=0;
QModelIndex d_index;
int m_index = 0;
CustomHeader::CustomHeader(Qt::Orientation orientation, QWidget * parent)
: QHeaderView(orientation, parent),mouseoverflg(false)
{
// Required to refresh button menu when the mouse leave the header.
//setAttribute(Qt::WA_Hover, true);
qDebug()<<"IN CUSTOM HEADER";
icon=false;
}
void CustomHeader ::testfun()
{
qDebug()<<"FOR UNFILTER ICONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN NNNNNNNNNNNNNNNNNNNNNNN";
filterIndex.remove(0,filterIndex.size());
}
void CustomHeader::UnfilterColumn()
{
qDebug()<<"In Unfilter";
QString un="";
qDebug()<<"UNFILTERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"<<un;
//emit unfilter(un);
//qDebug()<<"UNFILTERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR1 111111111111111111111111111111111"<<un;
int c=filterIndex.size();
filterIndex.remove(m_index);
/*for(int i=0;i<filterIndex.size();i++)
{
qDebug()<<"In Unfilter"<<i<<filterIndex.size();
// if(filterIndex.at(i)==logicalIndex)
int j=filterIndex[i];
qDebug()<<"j"<<j;
//SCH For removing filter specific column if(m_index==j)
filterIndex.remove(i);
qDebug()<<"In Unfilter1111"<<filterIndex.size();
}*/
//emit unfilter(un);
emit unfilter(m_index,un);
}
void CustomHeader::mousePressEvent ( QMouseEvent * event )
{
// qDebug()<<"mouse press beginssssssssssssssssssssssssssssssssssssssssss";
QHeaderView::mousePressEvent(event);
Items.clear();
comboBox=new QComboBox();
comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
Model = new QStandardItemModel;
qDebug()<<" In Custom Header";
for(int i =0; i<model()->columnCount();i++)
{
QString columnName = model()->headerData( i, Qt::Horizontal ).toString();
m_hideColumns[columnName]=i;
Item1 = new QStandardItem;
Item1->setText(columnName);
Item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
if(isSectionHidden (i))
Item1->setData(Qt::Checked, Qt::CheckStateRole);
else
Item1->setData(Qt::Unchecked, Qt::CheckStateRole);
Model->insertRow(i,Item1);
Items.push_back(Item1);
}
connect(Model, SIGNAL(dataChanged ( const QModelIndex&, const QModelIndex&)), this, SLOT(slot_changed(const QModelIndex&, const QModelIndex&)));
comboBox->setModel(Model);
int logicalIndex = logicalIndexAt(event->pos());
m_index = logicalIndex;
// qDebug()<<"LOGICAL INDEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"<<logicalIndex;
// qDebug()<<" emit indexOfColumn(logicalIndex);;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;"<<event->pos();
if (buttonMenuRect(logicalIndex).contains(event->pos())) {
QMenu menu(this);
emit indexOfColumn(logicalIndex);
// QAction *hideCol = menu.addAction("Hide column");
//QAction *sortAZ = menu.addAction("Sort sheet A->Z");
//QAction *sortZA = menu.addAction("Sort sheet Z->A");
QLineEdit *lineEdit = new QLineEdit(&menu);
lineEdit ->setStyleSheet("background-color: white;");
QWidgetAction *widgetAction = new QWidgetAction(&menu);
widgetAction->setDefaultWidget(lineEdit);
hideAction = new QWidgetAction(&menu);
hideAction->setDefaultWidget(comboBox);
connect(lineEdit,SIGNAL(textChanged(QString)),this ,SLOT(changedData(QString)));
QMenu *menu1 = new QMenu(QString("%1").arg("Search"),this);
QAction *unfilter = new QAction("UnFilter",this);
menu1->addAction(widgetAction);
// connect(menu1, SIGNAL(triggered(menu1)), this, SLOT(copy1()));
menu.addMenu(menu1);
menu.addAction(unfilter);
QMenu *hidemenu = new QMenu(QString("%1").arg("Hide Columns"),this);
hidemenu->addAction(hideAction);
menu.addMenu(hidemenu);
connect(unfilter,SIGNAL(triggered()),this,SLOT(Unf ilterColumn())) ;
//menu.addAction(hideCol);
// Disable hide column if only one column remains. Otherwise
// the gui is no more available to show them back.
// hideCol->setEnabled(hiddenSectionCount() < count() - 1);
QAction *res = menu.exec(mapToGlobal(event->pos()));
/*if (res == hideCol) {
hideSection(logicalIndex);
updateSection(logicalIndex-1);
} if (res == sortAZ)
static_cast<QSortFilterProxyModel*>(model())->sourceModel()->sort(logicalIndex, Qt::AscendingOrder);
if (res == sortZA)
static_cast<QSortFilterProxyModel*>(model())->sourceModel()->sort(logicalIndex, Qt::DescendingOrder);
if (res == sortAZ)
(model())->sort(logicalIndex, Qt::AscendingOrder);
if (res == sortZA)
(model())->sort(logicalIndex, Qt::DescendingOrder);
*/
}
// Catch previous arrow mouse click.
if (prevRect(logicalIndex).contains(event->pos())) {
showSection(logicalIndex - 1);
updateSection(logicalIndex - 2);
}
// Catch next arrow mouse click.
if (nextRect(logicalIndex).contains(event->pos())) {
showSection(logicalIndex + 1);
updateSection(logicalIndex + 2);
}
//qDebug()<<"Mouse press closeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
}
void CustomHeader::slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
// qDebug()<<"slottttttttttttttttttt changed";
QStandardItem* item = Items[topLeft.row()];
if(item->checkState() == Qt::Unchecked)
{
showSection(logicalIndex(m_hideColumns[bottomRight.data(Qt::DisplayRole).toString()]));
updateSection(logicalIndex(m_hideColumns[bottomRight.data(Qt::DisplayRole).toString()])-1);
//qDebug() << "Uncheckedddddddddddddddddddddddddddddddddddddddddd dddddddddddddddd!" ;
}
else if(item->checkState() == Qt::Checked)
{
hideSection(logicalIndex(m_hideColumns[bottomRight.data(Qt::DisplayRole).toString()]));
updateSection(logicalIndex(m_hideColumns[bottomRight.data(Qt::DisplayRole).toString()])-1);
qDebug()<<topLeft.data(Qt::DisplayRole)<<bottomRight.data(Qt::DisplayRole);
// qDebug() << "Checkedddddddddddddddddddddddddddddddddddddddddddd ddddddddddddddddddddddddddddd!";
}
}
void CustomHeader::copy1()
{
//qDebug()<<"COPY!!!!!!!!!!!!!!!!!!for searchhhhhhhhhhhhhhhhhhhhhh";
}
void CustomHeader::mouseMoveEvent(QMouseEvent * event)
{
qDebug()<<"mouse moveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeee event";
QHeaderView::mouseMoveEvent(event);
mouseoverflg=true;
// Required to refresh the button menu enable/disable state.
int logicalIndex = logicalIndexAt(event->pos());
updateSection(logicalIndex);
}
Added after 26 minutes:
void CustomHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
// qDebug()<<"paint";
//QHeaderView::paintSection(painter, rect, logicalIndex);
// if (!rect.isValid() || isSortIndicatorShown())
// return;
// qDebug()<<"Paint Section"<<logicalIndex;
// if(logicalIndex=0)
// return;
if (!rect.isValid())
return;
QPoint pos = mapFromGlobal(QCursor::pos());
if (rect.contains(pos) ) {
// const_cast<CustomHeader*>(this)->setSortIndicatorShown(true);
painter->save();
//const_cast<CustomHeader*>(this)->setSortIndicatorShown(true);
QHeaderView::paintSection(painter, rect, logicalIndex);
//const_cast<CustomHeader*>(this)->setSortIndicatorShown(false);
painter->restore();
if(filterIndex.contains(logicalIndex))
{
// qDebug()<<"drawMenuButton1 "<<"logicalIndex"<<logicalIndex;
drawMenuButton1(painter, logicalIndex, 1);
}
else
{
drawMenuButton(painter, logicalIndex, buttonMenuRect(logicalIndex).contains(pos));
}
if (isSectionHidden(logicalIndex - 1)) {
drawPrevButton(painter, logicalIndex);
}
if (isSectionHidden(logicalIndex + 1)) {
drawNextButton(painter, logicalIndex);
}
}
else
{
if(filterIndex.contains(logicalIndex))
{
QString columnName = model()->headerData( logicalIndex, Qt::Horizontal ).toString();
qDebug()<<"columnName"<<columnName;
//painter->drawText(rect,columnName);
painter->drawText(rect,Qt::AlignCenter,columnName);
QRect brect = buttonMenuRect(logicalIndex);
painter->setPen(Qt::white);
//painter->setBrush(QColor(46,92,127));
painter->setBrush(QColor(176,196,222));
//painter->setBrush(Qt::cyan);
painter->setPen(Qt::black);
painter->drawRect(brect);
//painter->setPen(Qt::black);
painter->drawLine(brect.left()+3, brect.top()+5, brect.right()-3, brect.top()+5);
painter->drawLine(brect.left()+4, brect.top()+6, brect.right()-4, brect.top()+6);
painter->drawLine(brect.left()+5, brect.top()+7, brect.right()-5, brect.top()+7);
painter->drawPoint(brect.left()+6, brect.top()+8);
painter->save();
}
else
{
// qDebug()<<visualIndexAt(logicalIndex)<<QHeaderView::logicalIndex ( 0 );
// qDebug()<<"logicalIndexxxxxyyyy"<<logicalIndex;
QHeaderView::paintSection(painter, rect, logicalIndex);
}
}
mouseoverflg=false;
return;
}
QRect CustomHeader::sectionRect(int logicalIndex) const
{
return QRect(sectionViewportPosition(logicalIndex), 0, sectionSize(logicalIndex), height());
}
QRect CustomHeader::buttonMenuRect(int logicalIndex) const
{
QRect sr = sectionRect(logicalIndex);
return QRect(sr.right() - 5 - 13, sr.center().y() - 6, 13, 13);
}
QRect CustomHeader::prevRect(int logicalIndex) const
{
if (isSectionHidden(logicalIndex))
return QRect();
QRect sr = sectionRect(logicalIndex);
return QRect(sr.left() + 1, sr.center().y() - 6, 13, 13);
}
QRect CustomHeader::nextRect(int logicalIndex) const
{
if (isSectionHidden(logicalIndex))
return QRect();
QRect sr = sectionRect(logicalIndex);
return QRect(sr.right() - 13, sr.center().y() - 6, 13, 13);
}
void CustomHeader::drawMenuButton1(QPainter *painter, int logicalIndex, bool enabled) const
{
int x = logicalIndex;
// storeindex.push_back(x);
// storeindex
QRect brect = buttonMenuRect(logicalIndex);
painter->setPen(enabled ? QColor(186,186,186) : QColor(223, 223, 223));
// painter->setBrush(QColor(646,646,646));
// painter->setBrush(Qt::cyan);
//painter->setBrush(QColor(46,92,127));
painter->setBrush(QColor(176,196,222));
painter->drawRect(brect.adjusted(0,0,-1,-1));
// painter->drawRect(brect);
painter->setPen(enabled ? QColor(71,71,71) : QColor(193, 193, 193));
painter->drawLine(brect.left()+3, brect.top()+5, brect.right()-3, brect.top()+5);
painter->drawLine(brect.left()+4, brect.top()+6, brect.right()-4, brect.top()+6);
painter->drawLine(brect.left()+5, brect.top()+7, brect.right()-5, brect.top()+7);
painter->drawPoint(brect.left()+6, brect.top()+8);
painter->save();
qDebug()<<"drawMenuButtonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn1111 111111111111111111111"<<logicalIndex;
}
void CustomHeader::drawMenuButton(QPainter *painter, int logicalIndex, bool enabled) const
{
QRect brect = buttonMenuRect(logicalIndex);
painter->setPen(enabled ? QColor(186,186,186) : QColor(223, 223, 223));
painter->setBrush(QColor(246,246,246));
painter->drawRect(brect.adjusted(0,0,-1,-1));
painter->setPen(enabled ? QColor(71,71,71) : QColor(193, 193, 193));
painter->drawLine(brect.left()+3, brect.top()+5, brect.right()-3, brect.top()+5);
painter->drawLine(brect.left()+4, brect.top()+6, brect.right()-4, brect.top()+6);
painter->drawLine(brect.left()+5, brect.top()+7, brect.right()-5, brect.top()+7);
painter->drawPoint(brect.left()+6, brect.top()+8);
}
void CustomHeader::drawPrevButton(QPainter *painter, int logicalIndex) const
{
QRect rect = prevRect(logicalIndex);
painter->setPen(QColor(71,71,71));
painter->drawLine(rect.left()+1, rect.center().y() - 3, rect.left()+1, rect.center().y() + 3);
painter->drawLine(rect.left()+2, rect.center().y() - 2, rect.left()+2, rect.center().y() + 2);
painter->drawLine(rect.left()+3, rect.center().y() - 1, rect.left()+3, rect.center().y() + 1);
painter->drawPoint(rect.left()+4, rect.center().y());
}
void CustomHeader::drawNextButton(QPainter *painter, int logicalIndex) const
{
QRect rect = nextRect(logicalIndex);
painter->setPen(QColor(71,71,71));
painter->drawLine(rect.right()-2, rect.center().y() - 3, rect.right()-2, rect.center().y() + 3);
painter->drawLine(rect.right()-3, rect.center().y() - 2, rect.right()-3, rect.center().y() + 2);
painter->drawLine(rect.right()-4, rect.center().y() - 1, rect.right()-4, rect.center().y() + 1);
painter->drawPoint(rect.right()-5, rect.center().y());
}
void CustomHeader::changedData(QString s)
{
if(s!="")
{
icon=true;
QStandardItemModel* pm = new QStandardItemModel();
//QStandardItem* pi = new QStandardItem(QIcon("./images/ceditor.png"));
//qDebug()<<"changed dataaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
if(!filterIndex.contains(m_index))
filterIndex.push_back( m_index);
emit searchString(m_index,s);
sflag=true;
s="";
}
}
FrozenTableWidget.h
/************************************************** **************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
************************************************** **************************/
#ifndef FREEZETABLEWIDGET_H
#define FREEZETABLEWIDGET_H
#include <CustomHeader.h>
#include <QTableView>
class FreezeTableWidget : public QTableView {
Q_OBJECT
public:
FreezeTableWidget(QAbstractItemModel * model,CustomHeader *m_headerN );
~FreezeTableWidget();
void horizantalHeader_Hide();
CustomHeader *m_header;
CustomHeader *m_header1;
protected:
virtual void resizeEvent(QResizeEvent *event);
virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
void scrollTo (const QModelIndex & index, ScrollHint hint = EnsureVisible);
private:
QTableView *frozenTableView;
void init();
void updateFrozenTableGeometry();
private slots:
void updateSectionWidth(int logicalIndex,int, int newSize);
void updateSectionHeight(int logicalIndex, int, int newSize);
};
#endif
FrozenTableWidget.cpp
/************************************************** **************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
************************************************** **************************/
#include <QtGui>
#include "freezetablewidget.h"
#include <CustomHeader.h>
FreezeTableWidget::FreezeTableWidget(QAbstractItem Model * model,CustomHeader *m_headerN)
{
setModel(model);
// m_header=m_headerN;
frozenTableView = new QTableView(this);
m_header = new CustomHeader(Qt::Horizontal);
m_header1= m_headerN;
frozenTableView->setHorizontalHeader(m_header);
init();
//connect the headers and scrollbars of both tableviews together
connect( m_header1,SIGNAL(sectionResized(int,int,int)), this,
SLOT(updateSectionWidth(int,int,int)));
connect(verticalHeader(),SIGNAL(sectionResized(int ,int,int)), this,
SLOT(updateSectionHeight(int,int,int)));
connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),
verticalScrollBar(), SLOT(setValue(int)));
connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
frozenTableView->verticalScrollBar(), SLOT(setValue(int)));
}
FreezeTableWidget::~FreezeTableWidget()
{
delete frozenTableView;
}
void FreezeTableWidget::horizantalHeader_Hide()
{
frozenTableView->horizontalHeader()->hide();
// QHeaderView *m_header1 = new CustomHeader(Qt::Horizontal);
// frozenTableView->setHorizontalHeader(m_header1);
}
void FreezeTableWidget::init()
{
frozenTableView->setModel(model());
frozenTableView->setFocusPolicy(Qt::NoFocus);
frozenTableView->verticalHeader()->hide();
// frozenTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
m_header->setResizeMode(QHeaderView::Fixed);
viewport()->stackUnder(frozenTableView);
frozenTableView->setStyleSheet("QTableView { border: none;"
"background-color: #8EDE21;"
"selection-background-color: #999}"); //for demo purposes
frozenTableView->setSelectionModel(selectionModel());
for(int col=1; col<model()->columnCount(); col++)
frozenTableView->setColumnHidden(col, true);
frozenTableView->setColumnWidth(0, columnWidth(0) );
frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
frozenTableView->show();
updateFrozenTableGeometry();
setHorizontalScrollMode(ScrollPerPixel);
setVerticalScrollMode(ScrollPerPixel);
frozenTableView->setVerticalScrollMode(ScrollPerPixel);
}
void FreezeTableWidget::updateSectionWidth(int logicalIndex, int, int newSize)
{
qDebug()<<"Update Section Width";
if(logicalIndex==0){
frozenTableView->setColumnWidth(0,newSize);
updateFrozenTableGeometry();
}
}
void FreezeTableWidget::updateSectionHeight(int logicalIndex, int, int newSize)
{
frozenTableView->setRowHeight(logicalIndex, newSize);
}
void FreezeTableWidget::resizeEvent(QResizeEvent * event)
{
qDebug()<<"ResizeEvent";
QTableView::resizeEvent(event);
updateFrozenTableGeometry();
}
QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers)
{
qDebug()<<"Move Cursor";
QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);
m_header->showSection(0);
if(cursorAction == MoveLeft && current.column()>0
&& visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
const int newValue = m_header1->horizontalScrollBar()->value() + visualRect(current).topLeft().x()
- frozenTableView->columnWidth(0);
m_header1->horizontalScrollBar()->setValue(newValue);
qDebug()<<newValue<<"newValue";
// m_header->viewport()->
// horizontalScrollBar()
}
qDebug()<<"current"<<current;
return current;
}
void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
if(index.column()>0)
QTableView::scrollTo(index, hint);
}
void FreezeTableWidget::updateFrozenTableGeometry()
{
// qDebug()<<"m_header->height()"<<m_header->height();
frozenTableView->setGeometry( verticalHeader()->width()+frameWidth(),
frameWidth(), columnWidth(0),
viewport()->height()+m_header1->height());
}
//m_header->height()
where i am doing wrong .. I Need it today itself . Please help me anyone .
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.