PDA

View Full Version : How To Apply Scroll Only for few columns of QTableview



RameshNani
16th December 2014, 09:22
Hi ,

I have to apply scroll for few columns in QTableView , I want to except some columns that are in one corner of QTableview , means i would like to left scroll from first 3 columns . Could anyone help us . I need it so badly ...

ChrisW67
16th December 2014, 09:27
You cannot do this with a single function call. Take a look at the Frozen Column (http://doc.qt.io/qt-5/qtwidgets-itemviews-frozencolumn-example.html) example for a way to "fake" it.

RameshNani
16th December 2014, 11:25
I tried same code but the scroll applied for all columns .

Added after 1 27 minutes:

I tried with two QTableviews , I was hide columns which are not want to freeze in 1st tableview , and in second table view I was hide freeze columns except all .
I attached both tableviews into QHBoxLayout . and I applied horizontal scroll only for 2nd tableview . But , My Problem is , QHBoxLayout split the space for two tableviews equally , I set SizePolicy Preferred , But still i have some space between two tableviews , How can I Manage this .
I am following this way because , I have added The above layout into QVBoxLayout , and second widget in this QVBoxLayout as Onother Tableview the Horizontal scroll of above table view depends on below scroll . I was Hide the above tableview scroll and when i move bottom tableview scroll am moving above tableview columns . So Please anyone Help Us .

ChrisW67
17th December 2014, 20:41
So your problem is really, "How do I fix the width of a table widget in a horizontal box layout?"
What have you actually tried? What does your code look like? What does it do?

BTW, it sounds like putting all the widgets in a grid layout would work well for you.

RameshNani
18th December 2014, 08:01
I am doing like below for freeze some columns from start ,

QTableview *childview = new QTableview();
childview ->horizantalscrollbar()->hide()
QTableview *FrozenchildView= new QTableView();
FrozenchildView->horizantalscrollbar()->hide();
FrozenchildView->verticalscrollbar()->hide();
FrozenchildView->setsizepolicy(QSizepolicy::Preffered,QSizepolicy:: Minimum)
QTableView * sumView= new QTableView();
QTableView *FrozenSumView=new QTableView();
model1=new qsqlquerymodel();
model2=new qsqlquerymodel();
QSortfilterproxymodel *proxy=new qsortfilterproxymodel();
model1->setQuery(sql);
proxy->setsourcemodel(model1);
childview->setmodel(proxy);
FrozenchildView->setmodel(proxy);
model2->setquery(sql);
qsortfilterproxymodel *sumproxy=new qsortfilterproxymodel();
sumproxy->setsourcemodel(model2);
sumView->setmodel(sumproxy);
FrozenSumView>setmodel(sumproxy);
QHBoxLayout *hlout = new QHBoxLayout();
hlout ->addwidget(FrozenchildView);
hlout ->addwidget(childView);
QHboxLayout *hloutsum = new QHboxLayout();
hloutsum->addwidget(FrozenSumView);
hloutsum->addwidget(sumView);
QVBoxlayout *vlot = new QVBoxlayout();
vlot ->addLayout(hlout);
vlot ->addLayout(hloutsum);
connect(sumView->horizontalScrollBar(), SIGNAL(valueChanged(int)),childView->horizontalScrollBar(), SLOT(setValue(int)));
connect(childView->verticalScrollBar(), SIGNAL(valueChanged(int)),FrozenchildView->verticalScrollBar(), SLOT(setValue(int)));
//In FrozenchildView I am showing only Frozen Columns, Remaining all columns are Hiden , and in childview am hiding FrozenColumns and showing all other columns.
//I was applied same for sumview,frozensumview .

These are my Functionalities , But doing like above the the layout had some space between two tableviews . Could any one help us .10822
I Attached Screen shot , Please find it .

RameshNani
18th December 2014, 13:51
If any Questions on above context , Please query to me , am doing like above .

ChrisW67
30th December 2014, 23:40
The spacing between widgets in a horizontal layout is contolled by QLayout::setSpacing() and around the outside of the layout by QLayout::setContentsMargins()

RameshNani
2nd January 2015, 11:54
We only able to apply setspacing for each widget in a layout right right ? Is there any way to apply directly on layout to set space between widgets with in that layout ?

anda_skoa
2nd January 2015, 12:50
Is there any way to apply directly on layout to set space between widgets with in that layout ?

Maybe you should read comment #7 again.

Cheers,
_

RameshNani
28th January 2015, 06:08
Hi ,

Now am able to freez columns , But i have one issue , that is when i use customized header the columns are freezing but the headers are moving . could any one help me . i need it very badly .

am using customized tableview frozentablewidget example code .

ChrisW67
28th January 2015, 11:15
Since the Frozen Column example headers work in the expected way you should probably study that.

RameshNani
29th January 2015, 12:14
HI , The frozentableview columns are frozen , but my cusom header override frozentableview header section , thats why the columns are moving , but i don't have any idea to distinguish frozentableview and custom header sections . the custom header painting section every time .

ChrisW67
29th January 2015, 19:40
The Frozen Column example (http://qt-project.org/doc/qt-4.8/itemviews-frozencolumn.html) does just that: the headers of the frozen column do not move when the rest of the table is scrolled right. Simply set your custom QHeaderView as the horizontal header of both table views.

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 .

ChrisW67
31st January 2015, 01:51
If you do not apply custom headers do the standard headers work as you expect they should?

If so, then the problem is in your header code and not the frozen column code.

RameshNani
31st January 2015, 10:06
Yeah , with this custom header i am facing this issue . when i use normal header view it's working fine , how could i modify my custom header , please help me , i need both functionalities .

ChrisW67
31st January 2015, 20:51
Here are possible problems.:

If the rect for the section being painted does not contain the cursor then you paint the section regardless of whether it is visible or not. Without delving into the Qt code I do not know if paintsection is called only for visible sections or not, but this is worth checking.
If the rect contains the pointer then you paint the default visual for the cell and then paint over it. You then paint the next/previous buttons based on whether the next logical index is visible or not. This is not the same as checking whether there is another visible column to the left or right.

Perhaps if you posted a screen shot of the problem it would be more apparent where the problem lay.

RameshNani
1st February 2015, 03:52
Hi ,

When i hide custom header am able to see frozen columns headers , but when i remove hide of custom header , am unable to see frozen headers , the frozen headers also overridden from custom header . even when i set some header sections hide in custom headers also the headers are painted , i don't know why they are painted , it might happen due to am not checking hidden sections in custom header paint section . I posted my code as it is . Could you show me any solution for this issue .

I Attached Screen shots of my issue . Before horizontal scroll and after scroll , the 1st two columns are frozen but the headers are moving unless of freez with columns by the reason of custom header .

ChrisW67
1st February 2015, 05:08
It looks like your base table custom header is stacked on top of the frozen column overlay's headers.

Move the call to set the horizontal header of the base table view into the FreezeTableWidget init() routine (it is in you MainWindow at the moment).
Move the call to set the horizontal header of the frozen column view into the FreezeTableWidget init() routine.
Apply both custom headers before you call "viewport()->stackUnder(frozenTableView);" to make sure the base table is underneat the frozen column overlay.

Help?

RameshNani
1st February 2015, 06:39
Sorry , I didn't understand could you please explain with my code snippet .

RameshNani
2nd February 2015, 05:55
HI , I setup base tableview header in init routin before setup FrozenTableView , and after setup custom header for both table views in init routin i applied viewport()->stackUnder(FrozenTableView) , but even also am facing same issue . could any one help me .

Added after 1 44 minutes:

Hi , I set the custom header for both base and frozen in init riutin and i applied viewport()->stackUnder(frozenTableView); but even still i have this issue .

HI , I setup base tableview header in init routin before setup FrozenTableView , and after setup custom header for both table views in init routin i applied viewport()->stackUnder(FrozenTableView) , but even also am facing same issue . could any one help me .

RameshNani
2nd February 2015, 08:55
I Applied like below in init routine

this->setHorizontalHeader(m_header1);
frozenTableView->setHorizontalHeader(m_header);
viewport()->stackUnder(frozenTableView);

RameshNani
3rd February 2015, 05:19
Anybody help me out from this issue .

RameshNani
3rd February 2015, 11:32
Hi Chris ,

Even when i use normal QHeaderView set to TableView also the Headers are moving Other than CustomHeader. means
QHeaderView *m_header = new QHeaderView(Qt::Horizantal);
basetableview->setHorizantalHeader(m_header );

if we not set any header to basetableview then only the headers are freezed . Could you help me ?

RameshNani
28th July 2016, 11:47
Hi Guru ,

I have one Problem with Frozen Columns .I am showing 2 FrozenColumns, I set TableView Geometry to show only 2 columns . But when i click on 2nd Column , the 2 nd column moved to 1st pos and 3rd column comes into the place of 2nd column position . But it should not happen , any suggestions from your side ?