Process recording of program development using Qt

First, the installation of qt

1. Download qt, select the version below qt6 from the following website, and select the win installation version, the size is about 2.4gb

https://download.qt.io/archive/qt/

In the installer choose to install all

Errors pop up during installation

Manually install it in the prompt folder and click ignore

Development can be done under Qt Creator

If you need to refer to an external library file, you need to add the absolute path of the external library file under the .pro file of the project before referencing

like:

INCLUDEPATH += E:\eigen-3.3.9

Second, the use of resources file

When you need to use pictures when designing ui, and when you need to load icons and music, you need to create a qrc resource file first

At the same time, put all resource files in the same file as main.cpp

First create a new folder under the .qrc file

Then right-click and click Add Existing File to add

If the .qrc file is too large, the compiler will report an error. In this case, you need to add the following code in .pro

CONFIG += resources_big

3. Various setting problems in Qt

1. Set the runtime icon and title

    this->setWindowTitle("Optimal Estimation Curriculum Design Program");
    setWindowIcon(QIcon(":/new/diqiu.ico"));

2. Set the icon in the resource manager

First create the rc file

Note: The suffix of .rc should be written by yourself

Put the icon in the project project folder

Write the following code in the .rc file:

IDI_ICON1 ICON  DISCARDABLE "diqiu.ico"

Add the following code to the .pro file:

RC_FILE += icon.rc

3. Set the static image you want to display, use here

Label control complete

Image loading: select the desired image in the resource file

Click the scaledContents below to adapt the image

4. Set up the software BGM

1) Fill in the code in the .pro file

QT       += multimedia

2) Fill in the code in the header file of the main page

#include <QSound>

3) Add the audio file in .wav format to the .qrc file

 

4) Write code inside the main page

    QSound::play(":/new/cszx.wav");

4. Simple chart display on sub-pages

Main interface code:

void MainWindow::Draw_Y()
{

    if(POS.size()==4)
    {
        QMessageBox::warning(this,"warn","Please solve first");
    }
    else
    {
    drawyy *drawyyw=new drawyy(POS,geshu,APPROX_POSITION);
    //Form *formw = new Form(POS,geshu,APPROX_POSITION);
    //formw=new Form;
    drawyyw->show();
    }
}

Subpage code:

drawyy::drawyy(Eigen::MatrixXd POS1, int geshu1, double *APPROX1, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::drawyy)
{
    ui->setupUi(this);
    this->setWindowTitle("Y drawing");//Set subpage title
    setWindowIcon(QIcon(":/new/diqiu.ico"));//set subpage icon
    POS=POS1;geshu=geshu1;//Reference main function data
    //qDebug()<<POS.size()<<endl;

    //qDebug()<<APPROX1[1]<<endl;
    //qDebug()<<POS1(0,0)<<endl;
    //qDebug()<<POS(0,0)<<endl;
    QChartView* ydraw=new QChartView(this);//Create a chart display class
    QChart* chart=new QChart();//Create chart class
    ydraw->setChart(chart);
    setCentralWidget(ydraw);//Set chart adaptation
    //formw->show();

    QLineSeries* Drawy=new QLineSeries;//Create a polyline class
    Drawy->setName("Y coordinate");//Set legend
    chart->addSeries(Drawy);
    for(qreal a1=0;a1<=geshu-1;a1++)
    {
        //Drawy->append(a1,(1));//POS(0,a1)/1000000)
        //*series << QPointF(11, 1)
        *Drawy<<QPointF(a1,POS(1,a1));//Set the data to display
    }
    QValueAxis* axisX=new QValueAxis;//set the X-axis
    axisX->setRange(1,geshu);//Set the X-axis range
    chart->setAxisX(axisX,Drawy);//Specifies that the drawing area is within range

    int APPROX2=APPROX1[1];

    QValueAxis* axisY=new QValueAxis;//Set the Y axis
    axisY->setRange(APPROX2-15,APPROX2+15);//(APPROX1[0]+15)/1000000,//Set the Y axis range (APPROX1[0]-15)/1000000
    chart->setAxisY(axisY,Drawy);//Specifies that the drawing area is within range
}


N: Bugs encountered that take a long time to debug

1. At the beginning of writing code, when calling the elements in the matrix, I often encountered the error of calling out of limit

Reason and solution: Eigen's matrix is ‚Äč‚Äčessentially an array by default from 0, so it also starts from 0. If MatrixXd X(2, 2) is defined, the first element is X(0, 0), The last element is X(1, 1)

2. Each loop outputs the same result when debugging

Cause and solution: wrongly +1 the count variable instead of the loop variable

At the beginning it was written for (int a1=0;a1<=i;i++)

It should actually be for(int a1=0 ; a1<=i; a1++)

3. Regarding the file output of C++, the escape character should be output with single quotation marks

4. Regarding the parameter transfer of Eigen, the pointer is generally used for parameter transfer.

Instructions:

Example of use:

Main function:

double APPROX_POSITION[3];//global variable
double X0=0, Y0=0, Z0=0;//Global variable to store receiver position
MatrixXd POS(2, 2);//Create coordinate result matrix
MatrixXd ANS(4, 1);
int geshu;

drawyy *drawyyw=new drawyy(POS,geshu,APPROX_POSITION);//function call

Subfunction:

drawxx::drawxx(Eigen::MatrixXd POS1, int geshu1, double *APPROX1, QWidget *parent) :
{
    POS=POS1;geshu=geshu1;
}

5. There is a bug that the coordinate axis range cannot be defined with variables when drawing with Qt

Qt's coordinate axis range definition function can only be defined with integers or string s when using variables. If you must use decimals, you can enter them manually, as shown in the figure

If as shown in the figure below, the variable must be an integer variable

6. setCentralWidget(); function

The setCentralWidget(); function belongs to the mainwindow class and cannot be used within the widget, so the mainwindow class should be used instead of the bit widget when drawing adaptive charts

Tags: Qt UI programming language

Posted by mmoranuk on Sat, 08 Oct 2022 23:06:55 +0530