Showing posts with label qtcpp. Show all posts
Showing posts with label qtcpp. Show all posts

Wednesday

Qt Yin Yang Painting with Changeable Size

Filename: main.cpp
Source code:

#include <QApplication> 
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsEllipseItem>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
 

    const int size = 500;
 

    QGraphicsScene scene;
    scene.setSceneRect( 0.0, 0.0, size+100, size+100 );
 

    QPen penWhite(Qt::white);
    QBrush brushWhite(Qt::white); //QBrush to fill-up shape with the color
 

    QPen penBlack(Qt::black);
    QBrush brushBlack(Qt::black); //QBrush to fill-up shape with the color
 

    //draw white big circle
    QPainterPath whiteBigCirlePainter;
    QPoint pointCircleCenter;
    pointCircleCenter.setX((size+100)/2);
    pointCircleCenter.setY((size+100)/2);
    whiteBigCirlePainter.addEllipse(pointCircleCenter, size/2, size/2);
    scene.addPath(whiteBigCirlePainter, penWhite, brushWhite);
 

    //draw black big semicircle to the right
    QPainterPath rightBlackSemicirclePath;
    rightBlackSemicirclePath.arcMoveTo((size+100)/2.0 - (size/2.0),
                                       (size+100)/2.0 - (size/2.0),
                                       size, size, 270);
    rightBlackSemicirclePath.arcTo((size+100)/2.0 - (size/2.0),
                                   (size+100)/2.0 - (size/2.0),
                                   size, size, 270, 180);
    scene.addPath(rightBlackSemicirclePath, penBlack, brushBlack);
 

    //draw black circle bottom
    QPainterPath blackCircleBottomPainter;
    QPoint pointCircleBottomCenter;
    pointCircleBottomCenter.setX((size+100)/2.0);
    pointCircleBottomCenter.setY((size+100)/2.0 + size/4.0);
    blackCircleBottomPainter.addEllipse(pointCircleBottomCenter, size/2/2, size/2/2);
    scene.addPath(blackCircleBottomPainter, penBlack, brushBlack);
 

    //draw white circle top
    QPainterPath whiteCircleTopPainter;
    QPoint pointCircleTopCenter;
    pointCircleTopCenter.setX((size+100)/2.0);
    pointCircleTopCenter.setY((size+100)/2.0 - size/4.0);
    whiteCircleTopPainter.addEllipse(pointCircleTopCenter, size/2/2, size/2/2);
    scene.addPath(whiteCircleTopPainter, penWhite, brushWhite);
 

    //draw white circle bottom eye
    QPainterPath whiteCircleBottomEyePainter;
    QPoint pointCircleBottomEyeCenter;
    pointCircleBottomEyeCenter.setX((size+100)/2.0);
    pointCircleBottomEyeCenter.setY((size+100)/2.0 + size/4.0);
    whiteCircleBottomEyePainter.addEllipse(pointCircleBottomEyeCenter, size/2/2/2/2, size/2/2/2/2);
    scene.addPath(whiteCircleBottomEyePainter, penWhite, brushWhite);
 

    //draw black circle top eye
    QPainterPath blackCircleTopEyePainter;
    QPoint pointCircleTopEyeCenter;
    pointCircleTopEyeCenter.setX((size+100)/2.0);
    pointCircleTopEyeCenter.setY((size+100)/2.0 - size/4.0);
    blackCircleTopEyePainter.addEllipse(pointCircleTopEyeCenter, size/2/2/2/2, size/2/2/2/2);
    scene.addPath(blackCircleTopEyePainter, penBlack, brushBlack);
 

    //draw big circle black outline
    QPainterPath bigCircleBlackOutlinePainter;
    bigCircleBlackOutlinePainter.addEllipse(pointCircleCenter, size/2, size/2);
    scene.addPath(bigCircleBlackOutlinePainter, penBlack, Qt::NoBrush);
 

    QGraphicsView view( &scene );
    view.setRenderHint(QPainter::Antialiasing); //antialias edges of primitives if possible
    view.setRenderHint(QPainter::HighQualityAntialiasing); //an OpenGL-specific rendering hint
    view.setWindowTitle("My Yin Yang Painting");
    view.show();
 

    return app.exec();
}
 
 
Project Output:

Sunday

Linux Mint Cinnamon: Install Qt for C++ GUI Programming

Start Menu > Administration > Software Manager

Search box > type > Qt-sdk
Double-click the Qt-sdk displayed result on one of the rows
Click the button Install
Wait until the download and installation actions completed

Start Menu > Programming > Qt Creator
Try to write some programs

Friday

Qt C++: Solution for “error: variable 'QPointer label' has initializer but incomplete type”

Qt C++: Solution for “error: variable 'QPointer<QLabel> label' has initializer but incomplete type”

C++ Code with Error message:

void MainWindow::on_pushButton_clicked()
{
  QPointer<QLabel> label = ui->label1;

  label->setText("Label 1 text changed");
}

Solution:

You need to include the “#include <QPointer>“ at the top section in this implementation file.
 

Qt C++: Solution for “error: only constructors take member initializers” and “warning: no return statement in function returning non-void [-Wreturn-type]”

Qt C++: Solution for “error: only constructors take member initializers” and “warning: no return statement in function returning non-void [-Wreturn-type]”

C++ Code with Error and Warning messages:

class ClassName
{
private:
  QString s;
  double d;
public:
  ClassKName(QString s="noName", double d=0.0) : s(s), d(d)
  {
  }
};

Solution:

The constructor name is different from the class name.
Rename the constructor name ClassKName to be the same with ClassName without the extra letter ‘K’.
 

Wednesday

Qt C++: Programming Fundamental Concepts: if-else statement

Program title: if-else statement

C++ Code:

#include <QApplication> 

#include <QDebug>
#include <QMainWindow> //QMainWindow *window
#include <QLabel>
#include <QHBoxLayout> //layout->addWidget(label)
#include <QWidget> //centralWidget->setLayout(layout)
#include <QInputDialog>
#include <QString>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow *window = new QMainWindow();

    QLabel *label = new QLabel("No Result");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(label);

    QWidget *centralWidget = new QWidget(window); //centralWidget parent is the window
    centralWidget->setLayout(layout);

    window->setCentralWidget(centralWidget);
    window->setWindowTitle("My Main Window");
    window->resize(400, 400);
    window->show();

    int n = QInputDialog::getInt(window, "Input Dialog",
                                 "Enter a percentage", 25, //25 is the default value
                                 0, 100, 1); //min, max, step

    if(n>=50) {
        qDebug() << "Percentage is greater than or equal to fifty";
        QString s = "Percentage is greater than or equal to fifty";        
        label->setText(s);
    }
    else {
        qDebug() << "Percentage is less than fifty";
        QString s = "Percentage is less than fifty";
        label->setText(s);
    }

    return app.exec();
} 

Program Output: Vertical Toolbar > Run 

Advantages:
-- input dialog: built-in spin box for integer input
-- input dialog: can set default value
-- input dialog: built-in regular expression and input validation
-- input dialog: prevent input error

Saturday

How to write a console application in C++ with Qt Creator?

Open Qt Creator.
Choose Menu > File > New File or Project.
Choose a template: Other Project > Qt Console Application.
Click Choose button to choose Qt Console Application.
Name the Qt Console Application project as qt_console_app_prj1.
Create the console application in D:\code_qt_folder path. Tick the box to use this path as default project location.
Click Next button.
Make sure Qt 4.7.3 for Desktop MinGW 4.4 (Qt SDK) debug and release boxes are ticked.
Click Next button.
Click Finish button.

Replace the entire main.cpp content with the following code:
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
    //Ctrl+/ = toggle between comment and uncomment selection

    string name;
    cout << "Enter your name: ";
    cin >> name; //World
    cout << "Hello, " << name << "!" << endl;

    system("pause");
    return 0;
}

Press Ctrl+S to save this file first.

Press Ctrl+R to run this file to get the Qt Console Application.

Notes:
You must create a project to run cpp files.

Interactive Proof System

Interactive proof systems were formulated in 1985 as published in the seminal research paper titled "The Knowledge Complexity of Intera...