- Cisco VPN client
- Gnokii + smsd
- Gpsdrive with Openstreetmap
- Grub2
- HP printer / scanner
- KDE / GNOME autostart
- Kubuntu / Ubuntu from USB stick
- LDAP
- Latest NVidia drivers
- Linux PC as router
- Microchip PIC
- NextWindow touchscreen
- OpenWrt
- Pinnacle PCTV Hybrid Stick Solo
- Qt
- Time handling
- VirtualBox
- Wacom tablet
- X11vnc
I recently started programming with Qt because I wanted to use the same source code for other platforms like Windows and Mac. Qt is based on C++. In Qt, methods can be slots which can be called automatically when a signal arrives to the class where the slot is declared in. There is a function called connect() which you can use to connect signals with slots. InstallTo install the SDK and demos on Ubuntu: aptitude install qdevelop qtcreator qt4-demos You can find the demos and examples in /usr/lib/qt4. There are 2 programming environments: qdevelop (older, with more features) and qtcreator (newer, with less features). The company recommends qtcreator. Double clicking on a .ui file within qdevelop or qtcreator, opens qt-designer which is a GUI designer. You can drag widgets onto the form, and in the context menu you can "go to slot", which creates a slot in your source code and brings up the editor with the cursor in the slot's body, so you can implement it right away. The .ui file is automatically converted to a C++ class in the source code, and the class you are editing is a subclass from it, so you won't lose any changes when code is regenerated using the .ui file. Buiding and running is done with the "play" buttons in the lower left corner. Windows specificFor Windows, go to the site and choose Downloads, LGPL / Free, Download Qt SDK for Windows. The install path is not allowed to have any spaces, so you cannot install Qt under "Program Files". Also, the installer says it needs 1Gb, but it actually needs twice as much while installing. Under Windows, stdin and stdout are not working, so don't bother using cin and cout. For debugging, #include <QDebug> and use qDebug() instead of cout. Additionally you can use qDebug the same way as printf(). To run the executable without the developing environment, you need to copy minwm10.dll, QtCore4.dll, QtGui4.dll and possibly other DLL files from C:\\qt\2009.003\qt\bin to the folder where your executable is located. Any output of qDebug() gets lost, because this only works within the developing environment. Programming tips:
Accessing databasesHere is some code for accessing a MySQL database:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "somename");
db.setHostName("hostname");
db.setDatabaseName("databasename");
if (!db.open("username", "password"))
{
qDebug("error: %s\n", db.lastError().text().toAscii().constData());
return;
}
qDebug("connection opened\n");
QSqlQuery query("select field1, field2 from sometable", db);
if (!query.isActive())
{
qDebug("error in query: %s\n", query.lastError().text().toAscii().constData());
return;
}
qDebug("processing");
// Show first field of all rows
while (query.next())
qDebug("%s\n", query.value(0).toString().toAscii().constData());
qDebug("Done\n");
query.finish();
db.close();
For GUI applications it is preferred to use the model/view approach that Qt provides. The next example shows the QSqlQueryModel which provides a read-only model for the result of an SQL query. This model is viewed using a tableView (which was created within qtcreator), but other view widgets may also be used. The code below shows the modified mainwindow.cpp.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlError>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionQuit_triggered()
{
this->close();
}
void MainWindow::on_okButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
QSqlQueryModel *model = new QSqlQueryModel;
db.setHostName("hostname");
db.setDatabaseName("databasename");
db.setUserName("username");
db.setPassword("password");
if (!db.open())
{
qDebug() << db.lastError().text();
return;
}
model->setQuery("SELECT name, address FROM adresses", db);
model->setHeaderData(0, Qt::Horizontal, tr("Name"), Qt::DisplayRole);
model->setHeaderData(1, Qt::Horizontal, tr("Address"), Qt::DisplayRole);
ui->tableView->setModel(model);
}
Windows specificFor MySql support under Windows, there is extra (10 minutes) of work to be done. The instructions below apply for Qt 4.5.
|
|||