QT简单类的使用

1 QTime

1.QTime构造函数
QTime::QTime(int h, int m, int s = 0, int ms = 0)
2.以毫秒级增减时间
QTime QTime::addMSecs(int ms) const
3.以秒级增减时间
QTime QTime::addSecs(int s) const
4.设置初始时间
bool QTime::setHMS(int h, int m, int s, int ms = 0)
//获取当前时间
[static] QTime QTime::currentTime()
	 QTime n(14, 0, 0);                // n == 14:00:00
	 QTime t;
	 t = n.addSecs(70);                // t == 14:01:10
	 t = n.addSecs(-70);               // t == 13:58:50
	 t = n.addSecs(10 * 60 * 60 + 5);  // t == 00:00:05
	 t = n.addSecs(-15 * 60 * 60);     // t == 23:00:00

QTime格式

hh:mm:ss.zzz    14:13:09.042
h:m:s ap        2:13:9 pm
H:m:s a         14:13:9 pm
//设置时间
	time.setHMS(0,0,0);
//将时间按指定的格式转换成字符串
    ui->label->setText(time.toString("hh:mm:ss:zzz"));

2 QTimer

2.1 QTimer一次性操作

在QT中,定时器可以做重复操作,也可以做一次性操作。
定时器一次性工作 ,比如页面设置广告,三秒后自动跳转到新的界面

QTimer::singleShot(3000, this, SLOT(startSystem()));

void Advertising::startSystem()
{
    LoginWin *login = new LoginWin;

    login->show();
    this->hide();
}

2.2 QTimer重复操作

[slot] void QTimer::start(int msec)
[slot] void QTimer::stop()
//超时信号
[signal] void QTimer::timeout()

使用步骤:
1.创建定时器对象
2.建立timeout超时信号的槽函数
3.启动定时器

	QTimer timer = new QTimer();
    //关联定时器超时信号与更新数据的槽函数
    connect(timer,&QTimer::timeout,
            this,&MainWindow::updataShow);
void MainWindow::updataShow()
{
    //以毫秒开始更新
    time = time.addMSecs(2);
    //将时间按指定的格式转换成字符串
    ui->label->setText(time.toString("hh:mm:ss:zzz"));
}

2.3 利用定时器在界面打印系统时间

在界面一直打印当前时间

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTime mtime = QTime::currentTime();

    ui->timeLabel->setText(mtime.toString("hh-mm-ss"));

    mtimer = new QTimer(this);
    connect(mtimer,&QTimer::timeout,
            this,&MainWindow::upTimeShow);
    mtimer->start(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::upTimeShow()
{
    ui->timeLabel->setText(QTime::currentTime().toString("hh-mm-ss"));
}

2.4 跑步计时器其他

//使能按钮
	ui->startBtn->setEnabled(true);
//关闭按钮
	ui->startBtn->setEnabled(false);
//在文本浏览器追加文本
	ui->textBrowser->append(text);

3 QDate

1.获取当前日期
[static] QDate QDate::currentDate()
Returns the current date, as reported by the system clock.
2.转换日期格式
QString QDate::toString(QStringView format) const
Returns the date as a string. The format parameter determines the format of the result string.

QDate格式

Format              	 Result
dd.MM.yyyy        		 20.07.1969
ddd MMMM d yy     	     Sun July 20 69
'The day is' dddd        The day is Sunday

4 QColor

4.1 QColor相关函数以及方法

	1.QString QColor::name() const
Returns the name of the color in the format "#RRGGBB";
	2.int QColor::red() const
Returns the red color component of this color.
	3.int QColor::green() const
Returns the green color component of this color.
	4.int QColor::blue() const
Returns the blue color component of this color.
	5.void QColor::setRed(int red)
Sets the red color component of this color to red.
Integer components are specified in the range 0-255.
	6.void QColor::setGreen(int green)
Sets the green color component of this color to green. 
	7.void QColor::setBlue(int blue)
Sets the blue color component of this color to blue. 	

4.2 颜色调试器其他

//设置滑动条的最大值为255,默认最大值为100
	ui->redSlider->setMaximum(255);
void MainWindow::on_redSlider_sliderMoved(int position)
{
    color.setRed(position);
    ui->redLabel->setText(QString::number(position));
    setColor(color);
}
void MainWindow::setColor(QColor &color)
{
	QString style = color.name();
	//将颜色的值打印出来 "#RRGGBB"
    ui->textEdit->setText(style);
	//设置样式表 改变背景色
    QString str = QString("background-color: rgb(%1, %2, %3);")
    					.arg(color.red()).arg(color.green())
						.arg(color.blue());
    ui->colorShowLable->setStyleSheet(str);
}

5 QString

	QString str = "hellozix";
[1]	获取字符串的长度
    str.length();
[2]获取字符串大小
    str.size();
[3]获取容量
    str.capacity();
[4]如果是0,返回true,否则返回false
    str.isNull();
[5]如果是空或者0,返回true,否则返回false
    str.isEmpty();
[6]清空str
    str.clear();
[7]设置字符的长度
    str.resize(20);
[8]将字符串的全部字符填充为'z'
    str.fill('z');
[9]填充3个字符's',并修改str字符串长度为3
    str.fill('i',3);
[10]从末尾追加字符穿
    str.append("hello");
[11]从前面加字符串
    str.prepend("hello");
[12]从字符串的末尾删除掉1个字符
    str.chop(1);
[13]删除掉下标2-3的字符
    str.remove(2,3);
[14]将数字10.7转化为字符串"10.7"
    str.setNum(10.7);
[15]将字符串转化为数字
    str.toDouble();
[16]拼接字符串,%1表示参数
    str = QString("%1,%2,%3").arg("Z").arg("Y").arg("X");
[17]字符串拼接
    str.sprintf("%d %s",1,"world");
[18]返回找到orl的下标位置/3
    str.indexOf("orl");
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 StripLinesCollection 的一个简单示例: ```cpp #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include <QtCharts/QValueAxis> #include <QtCharts/QChart> using namespace QtCharts; // 创建线系列 QLineSeries *series = new QLineSeries(); // 添加一些数据点 *series << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10, 5); // 创建图表并将线系列添加到图表中 QChart *chart = new QChart(); chart->addSeries(series); // 创建一个值轴并将其添加到图表中 QValueAxis *axisX = new QValueAxis; StripLinesCollection *stripLines = new StripLinesCollection(axisX); chart->addAxis(axisX, Qt::AlignBottom); // 创建一个StripLine并将其添加到StripLinesCollection中 QColor color(255, 0, 0, 100); QDateTime start(QDate(2021, 1, 1), QTime(9, 0, 0)); QDateTime end(QDate(2021, 1, 1), QTime(17, 0, 0)); QDateTimeAxisStripper *stripper = new QDateTimeAxisStripper(start, end, axisX); stripLines->add(stripper, color); // 将StripLinesCollection添加到图表中 chart->addAxisStripLines(stripLines); // 创建一个图表视图并将图表设置为它的模型 QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); // 显示图表视图 chartView->show(); ``` 在此示例中,我们创建了一个 `QLineSeries` 对象来存储一些数据点。然后,我们创建了一个 `QChart` 对象,并将线系列添加到图表中。接下来,我们创建了一个 `QValueAxis` 对象,并将其添加到图表中。我们还创建了一个 `StripLinesCollection` 对象,并将其绑定到值轴上。然后,我们创建了一个 `QDateTimeAxisStripper` 对象来表示一个时间间隔,并将其添加到 `StripLinesCollection` 中。最后,我们将 `StripLinesCollection` 添加到图表中,并将图表显示在 `QChartView` 中。 请注意,这只是一个简单的示例,StripLinesCollection 还有很多其他的功能和用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值