Tutorial :My listview is not gaining the keyboard focus in Qt


i am trying to operate the listview itesm through keyboard focus, its not moving.. can you folks suggest where i am wrong.

if i click on the listview from mouse, listview is gaining the focus. i dont no what is wrong.

 class Newlist : public QWidget     {  public:   Newlist(QWidget *parent = 0);      ~Newlist(){};    public:      QListView *list;      QStringListModel *model;       };    Newlist::Newlist(QWidget *parent)      : QWidget(parent)  {   list = new QListView(this);       list->setViewMode(QListView::ListMode);   list->setSelectionMode(QAbstractItemView::SingleSelection);     list->setMinimumSize(300,500);     model = new QStringListModel(this);     QStringList strlist;   strlist<<"Test"<<"fest"<<"mest";     list->setModel(model);     model->setStringList(strlist);       QModelIndex index = model->index(1,0);     list->setCurrentIndex(index);       QVBoxLayout *layout = new QVBoxLayout(this);     layout->addWidget(list);     setLayout(layout);     list->setFocus();  }    class Test : public QMainWindow  {    public:   Test(QWidget *parent = 0);      ~Test(){};    private:        Mylistview *newlist;      QVBoxLayout *layout;      QStackedWidget *stack;  };    Test::Test(QWidget *parent)      : QMainWindow(parent)  {     layout = new QVBoxLayout();   newlist = new Mylistview();    stack = new QStackedWidget(this);     stack->addWidget(newlist);     this->setCentralWidget(stack);    }      int main(int argc, char *argv[])  {      QApplication a(argc, argv);      Test test;      test.showMaximized();         return a.exec();  }  

Thanks in advance


I would change a few things... Here is a modification that's slightly better :

Test::Test(QWidget *parent): QMainWindow(parent)  {      pMylistview = new QListView();        QStringListModel* pListModel = new QStringListModel(this);        QStringList ModelStringList;      ModelStringList<<"Test"<<"fest"<<"mest";      pListModel->setStringList(ModelStringList);        pMylistview->setModel(pListModel);        QModelIndex Index = pListModel->index(1,0);      pMylistview->setCurrentIndex(Index);        this->setCentralWidget(pMylistview);  }  

Here are a few comments from your code :

1) I'm not sure you fully understand the power of Inheritance... If you choose to extend the class QListView, your new class "Newlist" IS a QlistView, so No need to create one in it... You can simply access the QListView's methods through your NewList class, because it's basically a QListView + some of your new features... Maybe You wanted to do something else and your class name was badly chosen or is it only in your example code but take care about that...

2) Why putting the Model in the view ?? Another view would have to know your first view to access the model ?? I guess you've been misslead by your first error... You thought about creating your view, then your model... It should be created in your form, or in a manager, a controller, etc... The main idea behind model-view is to separate those things, so you can connect multiple views on a model and therefore stay decoupled...

3) No need to create a layout if you use setCentralWidget...

I hope it's helping a bit !

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »