The Ultimate Qt Community site
Home News Forum Wiki Contest FAQ Links

Go Back   Qt Centre Forum > Qt > Qt Programming

Qt Programming General Qt programming issues.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 24th August 2006, 11:43
qiank128 qiank128 is offline
Beginner
 
Join Date: May 2006
Posts: 5
Thanks: 3
Thanked 1 Time in 1 Post
Default checkComboBox , how to do?

someone can help me?
Reply With Quote
  #2  
Old 24th August 2006, 11:55
jpn jpn is offline
Guru
 
Join Date: Feb 2006
Location: Tampere, Finland
Qt products used: Qt4
Qt platforms used: Unix/X11 , Windows
Posts: 6,128
Thanks: 36
Thanked 1,378 Times in 1,318 Posts
Default Re: checkComboBox , how to do?

If you mean something like this, it really shouldn't bee too hard since QComboBox allows you to use your own model (and view, which you probably don't event need..). You need to make the combobox non-editable and make the model to return Qt::ItemIsUserCheckable for all indexes QAbstractItemModel::flags(). I think all you need to do by hand is the filling of the line edit of the combobox.

Edit: Oh, and about the model. In my opinion, QStandardItemModel could be a good choice to start with, as QStringListModel doesn't support storing of check states..
__________________
J-P Nurmi
Reply With Quote
The following user says thank you to jpn for this useful post:
qiank128 (24th August 2006)
  #3  
Old 24th August 2006, 12:44
qiank128 qiank128 is offline
Beginner
 
Join Date: May 2006
Posts: 5
Thanks: 3
Thanked 1 Time in 1 Post
Question Re: checkComboBox , how to do?

Can you give me a example?

or some code

this is my code , but it doesnt work.

// CheckComboBox.h
#ifndef CHECKCOMBOBOX_H
#define CHECKCOMBOBOX_H

#include <QComboBox.h>
#include <QStandardItemModel>

class CheckModel : public QStandardItemModel
{
public:
CheckModel();

Qt::ItemFlags flags(const QModelIndex & index);

};

class CheckComboBox : public QComboBox
{
public:
CheckComboBox( QWidget * parent = 0 );

private:
};

#endif


// CheckComboBox.cpp
#include "CheckComboBox.h"

CheckModel::CheckModel() : QStandardItemModel()
{
}

Qt::ItemFlags CheckModel::flags(const QModelIndex & index)
{
return Qt::ItemIsUserCheckable;
}

CheckComboBox::CheckComboBox (QWidget * parent) : QComboBox(parent)
{
setEditable(false);
CheckModel *pModel = new CheckModel();
this->setModel(pModel);
}

Last edited by qiank128; 24th August 2006 at 12:54.
Reply With Quote
  #4  
Old 24th August 2006, 13:06
jpn jpn is offline
Guru
 
Join Date: Feb 2006
Location: Tampere, Finland
Qt products used: Qt4
Qt platforms used: Unix/X11 , Windows
Posts: 6,128
Thanks: 36
Thanked 1,378 Times in 1,318 Posts
Default Re: checkComboBox , how to do?

At least you need to insert a single column to your model:
Qt Code:
1
2
3
4
CheckModel::CheckModel() : QStandardItemModel()
{
    insertColumn(0); // important
}
You might also want to return a bit more flags (the base class implementation returns a combination of ItemIsEnabled and ItemIsSelectable):
Qt Code:
1
2
3
4
Qt::ItemFlags flags(const QModelIndex & index)
{
    return QStandardItemModel::flags(index) | Qt::ItemIsUserCheckable;
};
Also, when adding your items, you might need to initialize the check state:
Qt Code:
1
comboBox->setItemData(idx, Qt::Unchecked, Qt::CheckStateRole);
__________________
J-P Nurmi
Reply With Quote
The following user says thank you to jpn for this useful post:
qiank128 (24th August 2006)
  #5  
Old 24th August 2006, 14:33
qiank128 qiank128 is offline
Beginner
 
Join Date: May 2006
Posts: 5
Thanks: 3
Thanked 1 Time in 1 Post
Question Re: checkComboBox , how to do?

how to capture the QComboBox popup view clicked event?


CheckComboBox::CheckComboBox (QWidget * parent) : QComboBox(parent)
{
setEditable(false);
CheckModel *pModel = new CheckModel();
this->setModel(pModel);
this->setMinimumWidth(100);

// why this signal(clicked) can not be capture?

connect((const QObject *)QComboBox::view(), SIGNAL(clicked ( const QModelIndex & )), this, SLOT(_itemCB(const QModelIndex &)));
}


void CheckComboBox::_itemCB(const QModelIndex &index)
{
QMessageBox::warning( this, "clicked", "clicked" );
}

Last edited by qiank128; 24th August 2006 at 14:39.
Reply With Quote
  #6  
Old 24th August 2006, 15:27
jpn jpn is offline
Guru
 
Join Date: Feb 2006
Location: Tampere, Finland
Qt products used: Qt4
Qt platforms used: Unix/X11 , Windows
Posts: 6,128
Thanks: 36
Thanked 1,378 Times in 1,318 Posts
Default Re: checkComboBox , how to do?

Ahh, sorry. I didn't think of it that the view should remain visible when (un)checking items, or is that what you want?

Seems to be a bit complicated. At least much more complicated than I thought..

QComboBox installs an event filter on the view, which prevents the view from behaving normally in some situations. For example it filters all mouse release events and therefore the view never gets them and neither emits any clicked() signal..

I did a small example for you. The implementation is in a single file because I was so lazy.. All you have left is filling the combo's line edit correctly.
Attached Files
File Type: zip checkcombobox.zip (1,004 Bytes, 29 views)
__________________
J-P Nurmi
Reply With Quote
The following 2 users say thank you to jpn for this useful post:
qiank128 (25th August 2006), sector (24th August 2006)
  #7  
Old 25th August 2006, 02:42
qiank128 qiank128 is offline
Beginner
 
Join Date: May 2006
Posts: 5
Thanks: 3
Thanked 1 Time in 1 Post
Smile Re: checkComboBox , how to do?

// i have a mistake,

/*******why i can not capture the "MouseButtonRelease" event?

/*******can you?

/*******but i can use "MouseButtonPress" replace it,

/*******how to let the popup view always show , when i checked an item?

// view->installEventFilter(view); // <--- !!!
// view->viewport()->installEventFilter(view); // <--- !!!
// must behind the setView(view);
setEditable(false);
CheckModel* model = new CheckModel(this);
CheckView* view = new CheckView(this);
setModel(model);
setView(view);
// these 2 lines below are important and must be
// applied AFTER QComboBox::setView() because
// QComboBox installs it's own filter on the view
view->installEventFilter(view); // <--- !!!
view->viewport()->installEventFilter(view); // <--- !!!

Last edited by qiank128; 25th August 2006 at 03:39.
Reply With Quote
  #8  
Old 25th August 2006, 06:56
qiank128 qiank128 is offline
Beginner
 
Join Date: May 2006
Posts: 5
Thanks: 3
Thanked 1 Time in 1 Post
Smile Re: checkComboBox , how to do?

The attached file is CheckComboBox source code.

thanks jpn
Attached Files
File Type: zip CheckComboBoxSrc.zip (2.2 KB, 46 views)
Reply With Quote
The following user says thank you to qiank128 for this useful post:
fnmblot (25th August 2006)
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:41.


Powered by vBulletin Version 3.7.4 Copyright ©2000 - 2009, Jelsoft Enterprises Ltd., vRewrite 1.5 SEOed URLs completed by Tech Help Forum and Chalo Na.
© 2006–2009 Qt Centre - The Ultimate Qt Community site
Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.