2.QT字符串及一些基本操作

  • mainwindow.h
     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 namespace Ui {
     7 class MainWindow;
     8 }
     9 
    10 class MainWindow : public QMainWindow
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     explicit MainWindow(QWidget *parent = 0);
    16     ~MainWindow();
    17 
    18 private slots:
    19     void on_pushButton_clicked();
    20 
    21     void on_pushButton_2_clicked();
    22 
    23 private:
    24     Ui::MainWindow *ui;
    25 };
    26 
    27 #endif // MAINWINDOW_H

     

  • mainwindow.cpp
     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 #include <QDebug>
     4 
     5 MainWindow::MainWindow(QWidget *parent) :
     6     QMainWindow(parent),
     7     ui(new Ui::MainWindow)
     8 {
     9     ui->setupUi(this);
    10 }
    11 
    12 MainWindow::~MainWindow()
    13 {
    14     delete ui;
    15 }
    16 
    17 void MainWindow::on_pushButton_clicked()
    18 {
    19     QString qstr1;
    20     QString qstr2;
    21     qstr1 = ui->lineEdit->text();
    22     qstr2 = ui->lineEdit_2->text();
    23     QString qstr3 ;
    24    // qstr3 = qstr1 + qstr2
    25     qstr3.sprintf("%s%d%s","123",45,"abc");
    26     qDebug() << qstr3 << endl;
    27     ui->textEdit->setText(qstr3);
    28 }
    29 
    30 void MainWindow::on_pushButton_2_clicked()
    31 {
    32     QString qstr3=QString("%1 is %2").arg("hello").arg(123);
    33     ui->textEdit->setText(qstr3);
    34 }

     

posted @ 2018-04-07 15:51  喵小喵~  阅读(312)  评论(0编辑  收藏  举报