jiveaxe
1st August 2007, 08:55
Hi everybody,
i was learning qlistview and writing simple application for testing. First of all the sample code:
#include <QApplication>
#include <QVBoxLayout>
#include <QListView>
#include <QLabel>
#include <QStringListModel>
#include <QStringList>
class MyWidget: public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent=0);
private slots:
void currentSelected(const QModelIndex &);
private:
QListView *listView;
QLabel *selectedItemText;
QStringListModel *model;
};
MyWidget::MyWidget(QWidget *parent)
:QWidget(parent)
{
listView = new QListView;
selectedItemText = new QLabel;
model = new QStringListModel();
QStringList list;
list << "lemons" << "melons" << "oranges" << "pears"
<< "grapes" << "apples" << "potatoes";
model->setStringList(list);
listView->setModel(model);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(listView);
layout->addWidget(selectedItemText);
setLayout(layout);
connect(listView, SIGNAL(clicked(const QModelIndex&)),
this, SLOT(currentSelected(const QModelIndex &)));
}
void MyWidget::currentSelected(const QModelIndex ¤t)
{
selectedItemText->setText(current.data().toString());
}
int main(int argc, char *argv[])
{
QApplication app( argc, argv );
QWidget *window = new QWidget;
window->setWindowTitle("Sample ListView");
MyWidget *myWidget = new MyWidget;
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(myWidget);
window->setLayout(mainLayout);
window->show();
return app.exec();
}
#include "main.moc"
My questions are:
1) How start the application with some item already selected?
With the code above when an item is selected the QLabel is filled with the item data; but when the application starts no item is selected and the label is empty.
2) How bind the function currentSelected(const QModelIndex &) with up/down arrows keys?
In the application if I move the selection with the up/arrow keys the highlighted item changes but the label is not updated with the text of the highlighted item. I want the behavior of mouse single click with up/arrow keys.
3) How change the double-click event?
Now when an item is double-clicked the item goes in edit mode; i don't want this. Double-click like single-click is sufficient for me.
Hoping i was clear, thanks in advance for helping me.
Giuseppe
i was learning qlistview and writing simple application for testing. First of all the sample code:
#include <QApplication>
#include <QVBoxLayout>
#include <QListView>
#include <QLabel>
#include <QStringListModel>
#include <QStringList>
class MyWidget: public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent=0);
private slots:
void currentSelected(const QModelIndex &);
private:
QListView *listView;
QLabel *selectedItemText;
QStringListModel *model;
};
MyWidget::MyWidget(QWidget *parent)
:QWidget(parent)
{
listView = new QListView;
selectedItemText = new QLabel;
model = new QStringListModel();
QStringList list;
list << "lemons" << "melons" << "oranges" << "pears"
<< "grapes" << "apples" << "potatoes";
model->setStringList(list);
listView->setModel(model);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(listView);
layout->addWidget(selectedItemText);
setLayout(layout);
connect(listView, SIGNAL(clicked(const QModelIndex&)),
this, SLOT(currentSelected(const QModelIndex &)));
}
void MyWidget::currentSelected(const QModelIndex ¤t)
{
selectedItemText->setText(current.data().toString());
}
int main(int argc, char *argv[])
{
QApplication app( argc, argv );
QWidget *window = new QWidget;
window->setWindowTitle("Sample ListView");
MyWidget *myWidget = new MyWidget;
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(myWidget);
window->setLayout(mainLayout);
window->show();
return app.exec();
}
#include "main.moc"
My questions are:
1) How start the application with some item already selected?
With the code above when an item is selected the QLabel is filled with the item data; but when the application starts no item is selected and the label is empty.
2) How bind the function currentSelected(const QModelIndex &) with up/down arrows keys?
In the application if I move the selection with the up/arrow keys the highlighted item changes but the label is not updated with the text of the highlighted item. I want the behavior of mouse single click with up/arrow keys.
3) How change the double-click event?
Now when an item is double-clicked the item goes in edit mode; i don't want this. Double-click like single-click is sufficient for me.
Hoping i was clear, thanks in advance for helping me.
Giuseppe