views:

199

answers:

1

Hi,

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

+1  A: 

Hi,

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 !

Andy M
Hey andy..Thanks lot for your advice..i got it what exactly you ment.. you r rigth, nice man..ok well..now i what i do is, i will make my class to inherit from "QWidget" instead of "QlistView", so this time?if i do the way you suggest its working well, but in my application i cant do like this, i am using stacked widget, for this i need to create layout and put them in. so..now are you getting keyboard focus to listview, if i change the base class from "QListview" to "Qwidget" i dont want to create the listview in a class which inherit from mainwindow. its my aim so pls
Shadow
Take care, QListView IS a QWidget... So inheriting from QWidget to create a class that only contains a QListView seems strange to me... I guess you could simple add your QListview in your QStackedQwidget and give it focus... It should work...
Andy M
Andy, i have updated the my code above, placing a widget to stackedwidget. but i am facing problem still.i can't add the listview to stack, i need to write all the necessary stuffs regarding listview in a widget and then i have to add the same to stack..i will also tell you what operation i perform in the custom widget,create listview, set model, fill data, placing on a layout.
Shadow