Posts tagged 'qt'

CheckBox in QComboBox

Qt is one of the best GUI Widget cross-platform libraries that I ever worked with ;).

clip_image002

 

At some point, I was in need for a Combo box with a list of Checkbox item.  And that had not been provided as out-of- shelf widget.  For some reason, one of them as in the following quotes:

 “a combo box is a way to represent a single selection, not to hide other GUI elements”.

First let understand the QComboBox. The Combo box usually has two main parts:  current item view and items view (popup) .  Current view is view displaying the selected item.  While items view display a list of items to be select. 

Now let work with the items view to handle item as checkbox rather than a label ;).  In order to customize our items view we need to assignee an item delegate to the items view.   And that led us to create a customized (inherited) QItemDelegate let called CheckBoxListDelegate:

 

class CheckBoxListDelegate : public QItemDelegate

{

public:

    CheckBoxListDelegate(QObject *parent)

             : QItemDelegate(parent)

      {

            ;

      }

 

      void paint(QPainter *painter, const QStyleOptionViewItem &option,

                          const QModelIndex &index) const

      {

            //Get item data

            bool value = index.data(Qt::UserRole).toBool();

            QString text = index.data(Qt::DisplayRole).toString();

           

            // fill style options with item data

            const QStyle *style = QApplication::style();

            QStyleOptionButton opt;

            opt.state |= value ? QStyle::State_On : QStyle::State_Off;

            opt.state |= QStyle::State_Enabled;

            opt.text = text;

            opt.rect = option.rect;

 

            // draw item data as CheckBox

            style->drawControl(QStyle::CE_CheckBox,&opt,painter);

      }

 

    QWidget *createEditor(QWidget *parent,

             const QStyleOptionViewItem & option ,

             const QModelIndex & index ) const

      {

            // create check box as our editor

             QCheckBox *editor = new QCheckBox(parent);

             return editor;

      }

 

       void setEditorData(QWidget *editor,

                                                             const QModelIndex &index) const

       {

             //set editor data

             QCheckBox *myEditor = static_cast<QCheckBox*>(editor);

             myEditor->setText(index.data(Qt::DisplayRole).toString());

             myEditor->setChecked(index.data(Qt::UserRole).toBool());

       }

 

       void setModelData(QWidget *editor, QAbstractItemModel *model,

                                                            const QModelIndex &index) const

       {

             //get the value from the editor (CheckBox)

             QCheckBox *myEditor = static_cast<QCheckBox*>(editor);

             bool value = myEditor->isChecked();

 

             //set model data

             QMap<int,QVariant> data;

             data.insert(Qt::DisplayRole,myEditor->text());

             data.insert(Qt::UserRole,value);

             model->setItemData(index,data);

       }

 

       void updateEditorGeometry(QWidget *editor,

             const QStyleOptionViewItem &option, const QModelIndex &index ) const

       {

             editor->setGeometry(option.rect);

       }

 };

 

Then we need to tell our combo box to use that delegate as default delegate for the items view that it own.   Also we need to change the way it display the selected item.  Header of our CheckBoxList will look like:

class CheckBoxList: public QComboBox

{

      Q_OBJECT;

 

public:

      CheckBoxList(QWidget *widget = 0);

      virtual ~CheckBoxList();

      bool eventFilter(QObject *object, QEvent *event);

      virtual void paintEvent(QPaintEvent *);

      void SetDisplayText(QString text);

      QString GetDisplayText() const;

 

private:

      QString m_DisplayText;

};    

 

And the implementation:

CheckBoxList::CheckBoxList(QWidget *widget )

:QComboBox(widget),m_DisplayText(0)

{

      // set delegate items view

      view()->setItemDelegate(new CheckBoxListDelegate(this));

     

      // Enable editing on items view

      view()->setEditTriggers(QAbstractItemView::CurrentChanged);

     

      // set "CheckBoxList::eventFilter" as event filter for items view

      view()->viewport()->installEventFilter(this);

     

      // it just cool to have it as defualt ;)

      view()->setAlternatingRowColors(true);

}

 

 

CheckBoxList::~CheckBoxList()

{

      ;

}

 

 

bool CheckBoxList::eventFilter(QObject *object, QEvent *event)

{

      // don't close items view after we release the mouse button

      // by simple eating MouseButtonRelease in viewport of items view

      if(event->type() == QEvent::MouseButtonRelease && object==view()->viewport())

      {

            return true;

      }

      return QComboBox::eventFilter(object,event);

}

 

 

void CheckBoxList::paintEvent(QPaintEvent *)

{

    QStylePainter painter(this);

    painter.setPen(palette().color(QPalette::Text));

 

    // draw the combobox frame, focusrect and selected etc.

    QStyleOptionComboBox opt;

    initStyleOption(&opt);

 

      // if no display text been set , use "..." as default

      if(m_DisplayText.isNull())

            opt.currentText = "...";

      else

            opt.currentText = m_DisplayText;

    painter.drawComplexControl(QStyle::CC_ComboBox, opt);

 

    // draw the icon and text

    painter.drawControl(QStyle::CE_ComboBoxLabel, opt);

}

 

 

void CheckBoxList::SetDisplayText(QString text)

{

      m_DisplayText = text;

}

 

QString CheckBoxList::GetDisplayText() const

{

      return m_DisplayText;

}

 

And that’s it ;) see it in action:

      CheckBoxList list;

      list.addItem("Qt",true);

      list.addItem("CMake",true);

      list.addItem("10.99$",false);

      list.addItem("Da-Crystal",true);

 

clip_image004

clip_image002

 

Continue reading "CheckBox in QComboBox"

CMake & Qt4 (Q3Support)

Updated: this post is obsolete, go to  CMake & Qt4  Modules .

 

Assumption:

Let assume we have a legacy Qt3 system and has been ported to Qt4 with Q3Support Module successfully. Also we want to CMakify it.

 

Problem:

After following the instructions that is available on the Internet like QtNode. We MIGHT end up with the following compilation errors:

* Error(1): 'Q3somthing' : No such file or directory

    • 'Q3MainWindow': No such file or directory

* Error(2) :  'something' : is not a member of "Qt"

    • 'ToolBarDock' : is not a member of 'Qt'
    • 'Dock' : is not a member of 'Qt'

 

Solution:

in CMakeLists.txt

For Error(1) add the following :

# Qt Includes
    INCLUDE(${QT_USE_FILE})
    INCLUDE_DIRECTORIES(${QT_QT3SUPPORT_INCLUDE_DIR})

For Error(2) add the following:

# Qt Definitions
    ADD_DEFINITIONS(${QT_DEFINITIONS})
    ADD_DEFINITIONS(-DQT3_SUPPORT)

 

-DQT3_SUPPORT  almost two days to figure it out ending up with reading QT source-code.  So I hope this will help you.

 

Example:

I made example for this purpose also to show the simplicity and the power of CMake as cross-platform make system.

 

Windows Vista

Windows Vista & NMake(VS2008)

 

image

Linux & Make(gcc)

 

Example File: CMakifyQ3S

Continue reading "CMake & Qt4 (Q3Support)"

Qt/Windows & VC++ 2008 Express - Part1

In this part I'll show how to build Qt4 with MS Visual C++ 2008 (9.0) Express  ( yes, it out there Open-mouthed ) :

Requirement:

  1. MS Visual C++ 2008 (9.0) Express
  2. Qt/Windows Open Source Edition (.Zip version )

 

Steps:

  • decompress the file to folder of your choose let refer it as QtSource e.g:
    • C:\QtSource\

Update: From "Visual Studio 2008 Command Prompt":

  • now execute "configure.exe" , to prepare the source
    • C:\QtSource> configure
       

configure

  • now run 'nmake" command , to compile the source ( it will take long time , 1 hour - 4 hours) :
    • C:\QtSource> nmake

Building 

  • At thing point, you have almost everything ready Open-mouthed ,  now have a fun playing with the the  demos  ( QtSource\bin\qtdemo.exe ):
    • C:\QtSource> cd bin
    • C:\QtSource> qtdemo

Demos

 

Environment Configuration:

Continue reading "Qt/Windows & VC++ 2008 Express - Part1"

Qt4

Qt ( "cute" ) is a development framework that meant to develop cross-platform application. Moreover, it heavily used to develop GUI application (aka Widget). Also, Qt have been used in will-know applications like KDE , Opera , Google Earth , Skype , Qtopia and OPIE . Qt developed by Trolltech...

Continue reading "Qt4"