qt - How to get the Selected Row Number from QTableView? -


i new qt. using frozentabelwidget it's derived qtableview. how selected row number. if index changed(user click cell) need cell row number?

my code below:

//freezetablewidget.cpp  #include <qtgui> #include "freezetablewidget.h"   freezetablewidget::freezetablewidget(qabstractitemmodel * model)  {        setmodel(model);        frozentableview = new qtableview(this);        init();        connect(horizontalheader(),signal(sectionresized ( int ,int,int )), this,                slot(updatesectionwidth(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::init()  {        frozentableview->hidecolumn(0);        frozentableview->setmodel(model());        frozentableview->setfocuspolicy(qt::nofocus);        frozentableview->verticalheader()->hide();        frozentableview->horizontalheader()->setresizemode(qheaderview::fixed);        viewport()->stackunder(frozentableview);        frozentableview->setselectionbehavior(qabstractitemview::selectrows);        frozentableview->setedittriggers(qabstractitemview::noedittriggers);        frozentableview->sethorizontalscrollbarpolicy(qt::scrollbaralwaysoff);        frozentableview->setverticalscrollbarpolicy(qt::scrollbaralwaysoff);        frozentableview->show();        updatefrozentablegeometry();        sethorizontalscrollmode(scrollperpixel);        setverticalscrollmode(scrollperpixel);        frozentableview->setverticalscrollmode(scrollperpixel);  }   void freezetablewidget::currentchanged(const qmodelindex &current, const qmodelindex &previous)  {     qmessagebox::about(this,"test","hello");  }   void freezetablewidget::selectionchanged(const qitemselection &selected, const qitemselection &deselected)  {     qmessagebox::about(this,"test","123456");  }    void freezetablewidget::updatesectionwidth(int logicalindex, int, int newsize)  {        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)  {        qtableview::resizeevent(event);        updatefrozentablegeometry();   }   qmodelindex freezetablewidget::movecursor(cursoraction cursoraction,                                            qt::keyboardmodifiers modifiers)  {        qmodelindex current = qtableview::movecursor(cursoraction, modifiers);         if(cursoraction == moveleft && current.column()>0           && visualrect(current).topleft().x() < frozentableview->columnwidth(0) ) {               const int newvalue = horizontalscrollbar()->value() + visualrect(current).topleft().x()                                   - frozentableview->columnwidth(0);              horizontalscrollbar()->setvalue(newvalue);        }        return current;  }   void freezetablewidget::updatefrozentablegeometry()  {        frozentableview->setgeometry(framewidth(),                                      framewidth(), this->width(),                                      viewport()->height()+horizontalheader()->height());  }   //freezetablewidget.h   #ifndef freezetablewidget_h  #define freezetablewidget_h  #include <qtableview>   class freezetablewidget : public qtableview {       q_object   public:        freezetablewidget(qabstractitemmodel * model);        ~freezetablewidget();   protected:        virtual void resizeevent(qresizeevent *event);        virtual qmodelindex movecursor(cursoraction cursoraction, qt::keyboardmodifiers modifiers);        void currentchanged(const qmodelindex &current, const qmodelindex &previous);        void selectionchanged(const qitemselection &selected, const qitemselection &deselected);   signals:         void indexchanged();   private:        qtableview *frozentableview;        void init();        void updatefrozentablegeometry();   private slots:        void updatesectionwidth(int logicalindex,int, int newsize);        void updatesectionheight(int logicalindex, int, int newsize);   };  #endif   // main .cpp  #include <qapplication> #include <qstandarditemmodel> #include <qfile> #include <qdir> #include <qdebug> #include "freezetablewidget.h"  int main( int argc, char** argv ) {      qapplication app( argc, argv );     qstandarditemmodel *model=new qstandarditemmodel();      qstring line;     qstringlist list;      line = "#,name,score,sub1,sub2,sub4,sub5,sub6";     list= line.simplified().split(",");     model->sethorizontalheaderlabels(list);      int row=0;     line="aaa,bbb,ccc,ddd";     list= line.simplified().split(",");      foreach(qstring str,list) {         model->setitem(row ,0, new qstandarditem(qstring::number(row + 1)));         model->setitem(row ,1, new qstandarditem(str));         model->setitem(row ,2, new qstandarditem("600"));         model->setitem(row ,3, new qstandarditem("5"));         model->setitem(row ,4, new qstandarditem("60"));         model->setitem(row ,5, new qstandarditem("100"));         model->setitem(row ,6, new qstandarditem("100"));         model->setitem(row ,7, new qstandarditem("na"));     row++;     }      freezetablewidget *tableview = new freezetablewidget(model);      tableview->setwindowtitle(qobject::tr("frozen column example"));       tableview->resize(900,200);     tableview->show();     return app.exec(); } 

now if click cell try display qmessagebox, it's not working. please me fix problem.

i'm not sure you're trying freezetablewidget.

the basic problem you're creating two tables when create freezetablewidget, both of in same space 1 on top of other. you've got qtableview (frozentableview) placed on top of freezetablewidget (this).

that means selection changes happening in frozentableview, qtableview, , derived freezetablewidget won't method invocations from.

if want see this, in ::init() replace line:

frozentableview->show(); 

with

frozentableview->hide(); 

i can't see reason why need 2 tables on top of each other. if wasn't trying do, , want single table view (which recommend, unless have reasons need 2 of same tables on top of each other) can delete frozentableview member variable. can replace of code refers frozentableview (for example, of code in ::init sets behavior want) this.

i've included sample code below:

// freezetablewidget.cpp #include <qtgui> #include "freezetablewidget.h"   freezetablewidget::freezetablewidget(qabstractitemmodel * model)  {        setmodel(model);        init();        connect(horizontalheader(),signal(sectionresized ( int ,int,int )), this,                slot(updatesectionwidth(int, int, int)));  }   freezetablewidget::~freezetablewidget()  {  }   void freezetablewidget::init()  {        this->hidecolumn(0);        this->setmodel(model());        this->setfocuspolicy(qt::nofocus);        this->verticalheader()->hide();        this->horizontalheader()->setresizemode(qheaderview::fixed);        this->setselectionbehavior(qabstractitemview::selectrows);        this->setedittriggers(qabstractitemview::noedittriggers);        this->sethorizontalscrollbarpolicy(qt::scrollbaralwaysoff);        this->setverticalscrollbarpolicy(qt::scrollbaralwaysoff);        sethorizontalscrollmode(scrollperpixel);        setverticalscrollmode(scrollperpixel);  } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -