开发者

My listview is not gaining the keyboard focus in Qt

开发者 https://www.devze.com 2022-12-24 19:43 出处:网络
i am trying to operate the listview itesm through keyboard focus, its not moving.. can you folks suggest where i am wrong.

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 =开发者_Go百科 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 !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号