QT学习之旅 - 自定义控件


在这里插入图片描述
在这里插入图片描述
我们选择 widget

在这里插入图片描述
在这里插入图片描述
以上是选QT Froms的模板

我们可以选择QT设计师界面类
在这里插入图片描述

QT中horizontalLayout组件中调整长度

在这里插入图片描述

其中0,0是等宽

在其他组件中使用自定义控件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

关于ComboBox控件

在这里插入图片描述

ui->comboBox->addItem(QString("字符串"));

关于QStringList

QStringList longerList = (QStringList() << str1 << str2 << str3);

ico添加失败

在这里插入图片描述

xhcj.rc中添加这一段:

IDI_ICON ICON DISCARDABLE "ico的名称.ico"

在这里插入图片描述

获取windows端口号

pro文件(添加)

QT       += core gui serialport

cpp文件(添加)

#include<QtSerialPort/QSerialPort>
#include<QtSerialPort/QSerialPortInfo>
#include <QDebug>//这个可不添加
QList<QSerialPortInfo> serial_list = QSerialPortInfo::availablePorts();
QStringList com_list;
int serialsize = serial_list.size();
qDebug() << serial_list.size();//打印长度
for(int i = 0;i<serialsize;i++)
{
    
    
  QString flag = serial_list.at(i).description();
  //description()的功能:类型转换,将QSerialPortInfo类型转换成QString类型,description()的功能
  qDebug()<<i<<flag;
  if(flag == "USB 串行设备"){
    
    
    qDebug() << serial_list.at(i).portName();//输出端口号
    com_list = QStringList()<< serial_list.at(i).portName();
  }
}

关于表格

需要安装QWT,安装详细步骤可参考; QWT下载地址

在这里插入图片描述

设置背景布(ui界面)

QLinearGradient gradient(0,0,0,400);
gradient.setColorAt(1,QColor(90,90,90));
gradient.setColorAt(0.38,QColor(105,105,105));
gradient.setColorAt(1,QColor(70,70,70));
ui->qwtPlot->setCanvasBackground(QBrush(gradient));

设置网格

QwtPlotGrid *grid = new QwtPlotGrid;
grid->enableXMin( true );
grid->setMajorPen( Qt::gray, 0, Qt::DotLine );
grid->setMinorPen( Qt::darkGray, 0, Qt::DotLine );
grid->attach( ui->qwtPlot );

.h文件

#ifndef FFTLEARN_H
#define FFTLEARN_H

#include <QWidget>
#include "qwt_plot.h"
#include "qwt_plot_grid.h"
//#include <qwt_legend.h>
//#include <qwt_legend_label.h>
//#include <qwt_plot_legenditem.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_textlabel.h>
#include <qwt_plot_zoomer.h>
//#include <qwt_scale_draw.h>
//#include <qwt_plot_renderer.h>
//#include <QMessageBox>


namespace Ui {
    
    
class fftLearn;
}

class fftLearn : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit fftLearn(QWidget *parent = 0);
    virtual ~fftLearn();

private:
    int CurNum;
    QVector<QwtPlotCurve *> m_curve;
    QwtPlotZoomer *m_zoomer[2];
    QwtPlotTextLabel *InfoLabel;

private:
    Ui::fftLearn *ui;
    void InsertLegnd();
    void SetZoom();
private slots:
//    void showItem(const QVariant &itemInfo,bool on);
};

#endif // FFTLEARN_H

猜你喜欢

转载自blog.csdn.net/yasinawolaopo/article/details/131163104
今日推荐