Archive for the ‘C++’ Category

How to create a custom model for a QListView in Qt 4

Tuesday, August 2nd, 2011

Qt 4 provides a powerful model / view pattern to display custom data in various views – QListView, QTableView, etc. The problem is that the documentation for model/view programming is massive and it’s difficult to find simple examples on how to create custom models, custom views, etc.

So below is a simple example on how to create a custom model for a QListView. The model takes a std::vector of custom objects, and provides all the necessary methods. I’ve only included the minimal code so that you can get something working quickly, but of course the model can be further customized (see QAbstractListModel reference for instance).

EmployeeListModel.h

class EmployeeListModel : public QAbstractListModel {
	
	Q_OBJECT

public:

	explicit EmployeeListModel(const std::vector<Employee*>& employees, QObject* parent = 0);
	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	QVariant data(const QModelIndex &index, int role) const;
	
private:
	
	std::vector<Employee*> employees_;
	
};

EmployeeListModel.cpp

#include "EmployeeListModel.h"

EmployeeListModel::EmployeeListModel(const std::vector<Employee*>& employees, QObject *parent) : QAbstractListModel(parent) {
	employees_ = employees;
}

int EmployeeListModel::rowCount(const QModelIndex& parent) const {
	return employees_.size();
}

QVariant EmployeeListModel::data(const QModelIndex& index, int role) const {
	// Check that the index is valid and within the correct range first:
	if (!index.isValid()) return QVariant();
	if (index.row() >= decks_.size()) return QVariant();
	
	if (role == Qt::DisplayRole) {
		// Only returns something for the roles you support (DisplayRole is a minimum)
		// Here we assume that the "Employee" class has a "lastName" method but of course any string can be returned
		return QVariant(employees_.at(index.row())->lastName());
	} else {
		return QVariant();	
	}
}

Usage:

EmployeeListModel* model = new EmployeeListModel(employees);
ui->employeeListView->setModel(model);

Using Gettext with the Qt Framework

Monday, June 14th, 2010

The Qt Framework for some reason doesn’t provide any built-in support for Gettext, the quasi-standard for translating software programs. An alternative is provided, Qt Linguist, however there are several reasons why some may still prefer Gettext:

  • If your application is already partially translated using Gettext it’s much easier to keep on using it rather than converting all your mo/po files to Qt’s format.
  • Another significant advantage of Gettext is that it doesn’t have any dependencies (unlike Qt Linguist which creates a dependency to the Qt Framework) and thus allows creating more portable code.

So for these reasons, I’ve created a small library which parses and manages Gettext catalogues in Qt. The library includes a generic parser and a catalogue manager which can be used in any C++ project. On top of that, a Qt wrapper is provided for easy integration with the Qt framework.

The library can be downloaded from the Google Code project:

Download the Gettext library for the Qt Framework

Setting up and using the library is easy:

#include <QtGettext.h>

QtGettext::instance()->setLocale("cn_TW"); // Set the locale
QtGettext::instance()->setCatalogueName("catalogue"); // Set the name of the mo files
QtGettext::instance()->setCatalogueLocation("Data/Locales"); // Set the catalogue folder

QtGettext is now set to use the file in Data/Locales/cn_TW/catalogue.mo.

Also note that if this file is not available, QtGettext will try to load Data/Locales/cn/catalogue.mo (if available) as an alternative.

To mark a string for translation, simply call:

QtGettext::instance()->getTranslation("Some string to be translated");

Since this is rather long, a _() macro is also provided. It shortens the code to just:

_("Some string to be translated");

Delete a file to the recycle bin from C++

Friday, November 14th, 2008

Today, I was looking for a way to delete a file to the recycle bin using C++, but couldn’t find any simple example, so here it is:

SHFILEOPSTRUCT operation;
operation.wFunc = FO_DELETE;
operation.pFrom = "c:\\file\to\delete.txt";
operation.fFlags = FOF_ALLOWUNDO;

SHFileOperation(&operation);

Get the 48×48 or 256×256 icon of a file on Windows

Thursday, November 13th, 2008

Getting the 16×16 and 32×32 icons on Windows is relatively easy and is often as simple as one call to ExtractIconEx.

However, getting the extra large (48×48) and jumbo (256×256) icons introduced respectively by XP and Vista is slighly more complex. This is normally done by:

  1. Getting the file information, in particular the icon index, for the given file using SHGetFileInfo
  2. Retrieving the system image list where all the icons are stored
  3. Casting the image list to an IImageList interface and getting the icon from there

Below is the code I’m using in Appetizer to retrieve the extra large icon. If needed it can easily be adapted to get the jumbo icon.

Update: To do the same thing in C#, see the link in the comments below.

#include <shlobj.h>
#include <shlguid.h>
#include <shellapi.h>
#include <commctrl.h>
#include <commoncontrols.h>

// Get the icon index using SHGetFileInfo
SHFILEINFOW sfi = {0};
SHGetFileInfo(filePath, -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);

// Retrieve the system image list.
// To get the 48x48 icons, use SHIL_EXTRALARGE
// To get the 256x256 icons (Vista only), use SHIL_JUMBO
HIMAGELIST* imageList;
HRESULT hResult = SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList);

if (hResult == S_OK) {
  // Get the icon we need from the list. Note that the HIMAGELIST we retrieved
  // earlier needs to be casted to the IImageList interface before use.
  HICON hIcon;
  hResult = ((IImageList*)imageList)->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon);

  if (hResult == S_OK) {
    // Do something with the icon here.
    // For example, in wxWidgets:
    wxIcon* icon = new wxIcon();
    icon->SetHICON((WXHICON)hIcon);
    icon->SetSize(48, 48);
  }
}
Copyright © Pogopixels Ltd, 2008-2018