Dec. 8, 2015, 10:20 a.m.


ECHO, ECHO server, Qt, QTcpServer, QTcpSocket

To work with TCP / IP stack Qt provides QTcpServer, QTcpSocket, and QUdpSocket classes. For the first acquaintance with the work of the local network write Echo server. Task echo server to send back to the sender of the data obtained from it, as does the echo of a human voice. To connect to the server will be used telnet.

TELNET (TErminaL NETwork) — network protocol for the implementation of a text interface on the network (in modern form - using the TCP transport). The name «telnet» also have some tools that implement the client part of the protocol.

The proposed program QTcpServer class object will listen to one of the TCP/IP protocol stack ports from all hosts on the network. Listening port set by listen(), indicating the specified IP-address, or IP-address range, as well as the listening port.

When a client connects to the port we invoke signal newConnection() , which will connects to slot slotNewConnection() , the slot will be initiated by the client connection as QTcpSocket object on the server side using the method nextPendingConnection() , which returns a pointer to the object QTcpSocket .

Two slots will be connected to the new socket. First slotServerRead() slot is connected to the signal readyRead from the socket, and will be called in if the socket on the data came, who are willing to read. The second slot slotClientDisconnected() is connected to the signal disconnected() , which is called in the case when the client is disconnected from the server, and you must close the connection from the server side.

Project structure for work with QTcpServer

This will create a console application, so the classes like MainWindow in the annex will not be used.

  • - the profile of the project;
  • main.cpp - the main source file;
  • mytcpserver.h - header file server;
  • mytcpserver.cpp - file server source code;
Dec. 6, 2015, 9:47 a.m.


GameDev, QGraphicsItem, QGraphicsObject, QGraphicsScene, Qt

My attention was drawn to the fact that for working with signals and slots instead of a class inherited from QGraphicsItem and from QObject , you can use a class inherited from QGraphicsObject . Indeed, if a little rummage in the source QGraphicsObject , we find that it is a class inherited from QGraphicsItem and from QObject . That is also used multiple inheritance, but in this case all the bikes have written to us. Therefore, we try to work with this class as an example of the game mechanics.

Specifically, I propose to write a program in which we will move the hero mouse click on the graphic scene, as in any RPG like Diablo.

Project structure for work with QGraphicsObject

  • - the profile of the project;
  • main.cpp - the main source file;
  • widget.h - header file of the application window;
  • widget.cpp - file source code of the application window;
  • customscene.h - customized header graphic scene;
  • customscene.cpp - file source customized graphic scene ;
  • triangle.h - header file of hero class;
  • triangle.cpp - source file triangle class.
Dec. 4, 2015, 12:18 p.m.


figure, QGraphicsItem, QGraphicsScene, Qt, Qt уроки, square, triangle, ооп, Полиморфизм, пример

Polymorphism is one of the paradigms of OOP (Object Oriented Programming). Polymorphism implements the ability to provide base-class properties and methods, the implementation of which is not known for future classes that inherit from this class. That is the base class in this case, has all the necessary methods for the realization of tasks, but the specific implementation of certain methods is assigned to the descendants of the classes. And, as a rule, it is an abstract base class, then there are no objects on it.

For example, it is necessary to implement the program in which it is possible to draw a few geometric shapes with the mouse on the graphic scene , choosing the current figures with the buttons in the application. To do this, you can create several classes, each of which will implement all the necessary functionality, and actually duplicated in each class most of the code, making it more difficult and confusing design. Alternatively, you can create one base class, eg, Figure , which is inherited from QGraphicsItem , and implement all the basic functionality in this class, and then create three classes that will inherit from this class, but to implement them only a method of drawing the figure itself: Romb , Square , Triangle .

In the following figure shows the inheritance logic for the base class Figure. Applied as multiple inheritance class Figure for the implementation work with signals and slots .

Nov. 28, 2015, 10:06 a.m.


Navigation Drawer, Qt, QtQuick, QML, Qndroid

One of the key controls in the Android app is the Navigation Drawer , but QML component is not ready for this item, but the guys from Cutehacks made your Navigation Drawer , the code of the component is laid out on githabe. I have long wanted to run the code on an Android device and live my hands finally to have reached it.

I studied in detail the code and slightly it corrected, because that version was some discrepancy Material Design in the sense that the socket was revealed 78 per cent of the width of the screen, regardless of orientation. A Material Design recommends portrait disclose Navigation Drawer so that it did not reach the opposite edge 56 dip in the case with smartphones and 64 dip in the case of tablets, but make at least for smartphones, and in landscape orientation was not more 320 dip width. What I corrected, and drank a small part of the unnecessary at the moment and a little bit of code rename the variable by itself.

With regard to the magnitude of the dip , that is independent of the pixel density of the display device, it is a question of the correct scaling of interface elements.

I bring to your attention an example of the use of the Navigation Drawer to change the three fragments in the Loader object using three menu items that will be located in the Navigation Drawer.

The structure of the project to work with the Navigation Drawer

The structure of the project will be similar to the structure of the draft articles on the working with the component Loader .

  • - the profile of the project;
  • main.cpp - the main file of the application source code;
  • main.qml - basic qml code file;
  • Fragment1.qml - the first fragment for replacement Loader;
  • Fragment2.qml - second fragment;
  • Fragment3.qml - a third fragment.
  • NavigationDrawer.qml - the object Navigation Drawer.
Nov. 27, 2015, 9:54 a.m.


Qt, QML, QtQuick, Loader

For the organization of the dynamic components of the change is convenient to use a Loader component, which is included in QML QtQuick and is a container for components in your application, let's say that you need to periodically replace the interface.

To draw an analogy, for example, with the development of Java for Android , there is a system of fragments, which can also be replaced in the container for them, following the logic of your application. For example, we click on the button and in a particular container, we replaced one fragment of another, and if we click on another button, then there is a third fragment, which replaces the second fragment is.

Therefore, make an application that will have 5 buttons and pressing each button in the Loader fragments will vary.

The structure of the project to work with the Loader

  • - the profile of the project;
  • main.cpp - the main file of the application source code;
  • main.qml - basic qml code file;
  • Fragment1.qml - the first fragment for replacement Loader;
  • Fragment2.qml - second fragment;
  • Fragment3.qml - a third fragment.
Nov. 25, 2015, 9:43 a.m.



In that case, if you need to unload the table data from an application to a CSV file, the interface is more and is written in the QML, then hasten to this article, to give a little hint.

For example, in an article on work with SQLite database in the annex to the QML database was created, to which is added the data and removed from it. At the same time they appear in the TableView, which was already part of the interface written in QML. And to display the table used by the data model to inherit from QSqlQueryModel .

I propose to use the source code of the project and expand it so that there was a button in the application to export data to a CSV file.

What is a CSV format

CSV ( Comma-Separated Values ) — text format for presenting tabular data. Each line of the file - this is one line of the table. The values of individual columns are separated by a separating character (delimiter) - a comma (,). However, most of the programs is free treats CSV standard and allow the use of other characters as the delimiter.

In general, the essence of the work is reduced to ensure a method to our data model that will pull data from this model and a semicolon each cell in the row, and each row share a newline character "\ n".

Nov. 25, 2015, 8:17 a.m.


BLOB, QPixmap, Qt, SQL, SQLite, база данных, изображение

Images in the database can be stored in BLOB format ( Binary Large Object ), that is an array of binary data format. BLOB format is also suitable for storing audio and video data in databases.

Consider the saving and loading of the image from the database as an example of the next application that screenshot of your computer screen will be saved in the database and displayed in a special a QLabel , when the corresponding entry is selected in a QTableView , which will display all the records of the images stored in the database .

Thus we have a database table with the following fields:

  • id (INTEGER) - id;
  • Name (VARCHAR(255)) - the name stored in the database file data;
  • Pic (BLOB) - a field for storing images in the database.

If you do not go into detail in the lesson, the most valuable information on this topic at the end of mainwindow.cpp file in the following methods:

  • MainWindow::on_screenButton_clicked()
  • MainWindow::slotCurrentPic(QModelIndex index)

Project structure

  • - the profile of the project;
  • database.h - header file wrapper class for working with the database;
  • database.cpp - file source wrapper class codes for the work with the database;
  • mainwindow.h - header file of the main application window;
  • mainwindow.cpp - file source code of the main application window;
  • mainwindow.ui - form the main application window;
  • main.cpp - the main file of the application source code.
Nov. 24, 2015, 9:11 a.m.


База данных, SQLite, Qt, QML, Android, DataBase, Model, View, MVC

A small example to work with the database in QML Qt. This lesson brings together information on the use of signals and slots in the Qt QML , access to C++ classes from QML layer implementation of the application interface on QML, as well as the implementation of the model/view for the database tables.

The database contains a table with a list of people, which has four columns:

  • id (INTEGER) - a unique record number;
  • FirstName (VARCHAR (255)) - First Name;
  • SurName (VARCHAR (255)) - Last Name;
  • Nik (VARCHAR (255)) - Nik name.

The application must implement removing and adding records to the database through the application interface. To add records to the database three fields for data entry will be used and a button that initiates the addition of data to an SQL database. Adding entries to the table through a wrapper class designed for this application is present in metodom.Takzhe ListModel class that implements the model of data representation for the information display in the TableView in QML layer.

Nov. 20, 2015, 10:23 a.m.


DECIMAL, INTEGER, MS SQL, Qt, SQL, типы данных

In the process of application development for Qt, which will have to work with databases, you must take into account the nuance that Qt driver target Databases may not support all types of data that you can lay in your database. For example, one such example, which faced one of the Readers site, this is the problem with data type mapping DECIMAL through QSqlTableModel . At that time, both in the development of applications in the C++ Builder, this type of data displayed in the fine.

And the work was done with the database MS SQL database for which an ODBC driver used. The results of the applications on C ++ Builder and Qt are shown in the following figures. The difference in the external tables, as is that in Qt applications are no values in the view, where the type of data in the database has been declared as DECIMAL .

Nov. 18, 2015, 10:14 a.m.


QSqlTableModel, Qt, paint

In the lesson on working with QSqlTableModel was detailed mapping table data from a SQL database. But with the question from one of the readers, do a small addition to this example.

The question was how to paint an entire row depending on the value in one of the columns. In the example of operation performed with the addition of QSqlTableModel data base rows with the date, time, and a pseudo-random number notification of the pseudo-random number. In this regard, the lines were selected as test lines, where the pseudo-random number is equal to 41.

To do this, you must inherit from QSqlTableModel class and override the method QSqlTableModel::data() . Then use as a data model, a new class of heir.


Hello, Dear Users of EVILEG!!!

If the site helped you, then support the development of the site financially, please.

You can do it by following ways:

Thank you, Evgenii Legotckoi

Nov. 17, 2019, 2:25 a.m.
Vladimir Zhitkovsky

Qt - Test 001. Signals and slots

  • Result:78points,
  • Rating points2
Nov. 17, 2019, 2:20 a.m.

C++ - Test 002. Constants

  • Result:50points,
  • Rating points-4
Nov. 17, 2019, 2:15 a.m.

C++ - Test 001. The first program and data types

  • Result:53points,
  • Rating points-4
Last comments
Nov. 17, 2019, 2:25 p.m.

Шикарно! Как обычно всё объясняется чётко, ясно и, по-возможности, кратко. Побольше бы контента по PyQt5...
Nov. 16, 2019, 4:16 p.m.
Gibraltar Vol'framov

Разрешите узнать, как вы получили qt+qml приложение размером в 10мб(даже релизных). Дело в том, что пустое приложение, хоть и со стек вью, обходится мне на все 40мб пространства. Или же вы воспо…
Nov. 9, 2019, 8:28 a.m.

спасибо ещё раз. огромное, за уделённое время
Nov. 9, 2019, 8:24 a.m.

Спасибо Вам большое. Буду изучать.
Nov. 9, 2019, 5:58 a.m.
Evgenij Legotskoj

Добрый день. По первым двум вопросам вы найдёте ответ в этой статье - PyQt5 - Урок 008. Работа с QTableWidget (Обновление урока 006) Что касается последнего вопроса, то я вам…
Now discuss on the forum
Nov. 17, 2019, 12:30 p.m.
Mihail Sermjazhko

Взял этот код и сделал так m_Barcode = new Code128Item(); m_Barcode->setWidth( 200 ); m_Barcode->setHeight( 80 ); m_Barco…
Nov. 16, 2019, 5:58 a.m.

Благодарю! Пойду читать всю эту документацию, чтобы понять, как это вообще работает и дальше мучать свой прогресс бар.
Nov. 15, 2019, 7:37 a.m.

Евгений, почитав про эту проблему пришел к выводу, что либо нужно говорить очередь, либо все вернуть из библиотеки (dll в моем случае) в приложение, потому что в приложении все работает просто з…
Nov. 15, 2019, 6:06 a.m.
Evgenij Legotskoj

Ну тогда не знаю )) Я большую часть времени на C++ с Qt работаю, а PyQt5 у меня боком. Так что, чем можем помочь ))
Nov. 15, 2019, 5:18 a.m.

Да, пробовал, с преобразованием int array[]={1,2,3,4,5,6,7} в jintArray(array). Если так, то программа компилируется без ошибок и предупреждений, но вываливается с ошибкой времени выполнени…
© EVILEG 2015-2019
Recommend hosting TIMEWEB