QT学习笔记

获取本机的IP地址

1 工程配置文件pro文件中添加库 QT += network
2 引入头文件 #include
3 编写代码

QString MainWindow::getLocalHostIP()
{
    QString ip = "";
    QString localhostName = QHostInfo::localHostName();
    QHostInfo info = QHostInfo::fromName(localhostName);
    foreach(QHostAddress address,info.addresses())
    {
        if(address.protocol() == QAbstractSocket::IPv4Protocol)
            ip = address.toString();
    }
    qDebug() << ip;
    return ip;
}

获取当前时间

1 引入头文件 #include
2 编写代码

QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd"); //设置显示格式
qDebug() << str;

3 时间格式相关的内容
QString QDateTime::toString ( const QString & format ) const
Returns the datetime as a string. The format parameter determines the format of the result string.
These expressions may be used for the date:

  • Expression Output
  • d - the day as number without a leading zero (1 to 31)
  • dd - the day as number with a leading zero (01 to 31)
  • ddd - the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
  • dddd - the long localized day name (e.g. 'Monday' to 'Qt::Sunday'). Uses QDate::longDayName().
  • M - the month as number without a leading zero (1-12)
  • MM - the month as number with a leading zero (01-12)
  • MMM - the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().
  • MMMM - the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().
  • yy - the year as two digit number (00-99)
  • yyyy - the year as four digit number

These expressions may be used for the time:

  • ExpressionOutput
  • h - the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
  • hh - the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
  • m - the minute without a leading zero (0 to 59)
  • mm - the minute with a leading zero (00 to 59)
  • s - the second without a leading zero (0 to 59)
  • ss - the second with a leading zero (00 to 59)
  • z - the milliseconds without leading zeroes (0 to 999)
  • zzz - the milliseconds with leading zeroes (000 to 999)
  • AP - use AM/PM display. AP will be replaced by either "AM" or "PM".
  • ap - use am/pm display. ap will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.
Example format strings (assumed that the QDateTime is 21 May 2001 14:13:09):

  • dd.MM.yyyy - 21.05.2001
  • ddd MMMM d yy - Tue May 21 01
  • hh:mm:ss.zzz - 14:13:09.042
  • h:m:s ap - 2:13:9 pm

执行Windows cmd命令

1 引入头文件 #include
2 编写代码

QProcess p(0);
p.start("ipconfig");
p.waitForStarted();
p.waitForFinished();
qDebug()<

创建Telnet连接

Qt自己有个QtTelnet ,看了半天不会用,加上头文件后,一直报错,找不到文件,网上也找不到说明,却搜到很多人说这个有毛病,不能用,最后终于找见一个大牛的库,直接上链接:[https://github.com/triochi/qttelnet-2.1-opensource](https://github.com/trio

newpic.jpg

chi/qttelnet-2.1-opensource),下载下来可以直接运行。

你可能感兴趣的:(QT学习笔记)