预览QtCreator中的界面,使用警告弹窗,安装ImageWatch,QString与QByteArray互相转换,获取当前时间,int与QString互转
预览QtCreator中的界面
如何预览QtCreator的界面?当在QtCreator界面编辑器中放置好布局和控件之后,我们需要看一下摆放的对不对,VC中有预览的功能,就是不需要编译和运行程序就能看到UI,QT中也有类似的功能,但是查找了所有菜单也没有发现预览功能。然后百度了好久,才发现有一个组合键可以实现预览功能:
Alt + Shift + R。
获取当前时间
时间日期是经常遇到的数据类型,Qt 中时间日期类型的类如下:
QTime:时间数据类型,仅表示时间,如15:23:13
QDate:日期数据类型,仅表示日期,如2017-4-5
QDateTime:日期时间数据类型,表示日期和时间,如2022-03-23
08:12:43
QDateTime curDateTime=QDateTime::currentDateTime (); ui->timeEdit->setTime (curDateTime.time ()); ui->editTime->setText (curDateTime.toString ("hh:mm: ss11)); ui->dateEdit->setDate(curDateTime.date()); ui->editDate->setText(curDateTime.toString(" yyyy-MM-dd")); ui->dateTimeEdit->setDateTime(curDateTime); ui->editDateTime->setText(curDateTime.toString(" yyyy-MM-dd hh:mm:ss"));
int与QString互转
int a = 123456 ;QString b; b = QString::number (a,10 ,5 ); int a = 123456 ;QString b = QString ("%1" ).arg (a); QString c = "123456" ; int d = c.toInt ();
使用警告弹窗
auto button = QMessageBox::critical (this ,tr ("Open Failed!" ) ,tr ("Do you want to delete this connection?" ) ,QMessageBox::Ok|QMessageBox::No,QMessageBox::Ok ); if (QMessageBox::Ok == button) "do some thing" ;
QString与QByteArray互相转换
QString转QByteArray方法
QString str ("hello" ) ; QByteArray bytes = str.toUtf8 (); QString str ("hello" ) ; QByteArray bytes = str.toLatin1 ();
QByteArray转QString方法
QByteArray bytes ("hello world" ) ;QString string = bytes; QByteArray bytes ("hello world" ) ;QString string; string.prepend (bytes);
安装ImageWatch
ImageWatch需要调试器来调用,插件编译过程中需要用到qt5库
sudo apt-get install gdb qt5-default git clone https://github.com/csantosbh/gdb-imagewatch cd gdb-imagewatchsudo ./configure_ubuntu_16.sh
ubuntu21.04开始移除了qt5-default,使用sudo apt-get install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools
代替
设置QtCreator调用
Qt->工具->选项->调试器->GDB->启动时载入.gdbinit文件(选择性使用gdb-imagewatch)
使用qtcreator新建opencv工程,CMakeLists.txt如下编写,引入opencv库
cmake_minimum_required (VERSION 3.5 )project (optFlow LANGUAGES CXX)set (CMAKE_CXX_STANDARD 11 )set (CMAKE_CXX_STANDARD_REQUIRED ON )find_package (OpenCV REQUIRED)include_directories (${OpenCV_INCLUDE_DIRS} )link_directories (${OpenCV_LIBRARY_DIRS} )add_executable (${PROJECT_NAME} main.cpp)target_link_libraries (${PROJECT_NAME} ${OpenCV_LIBS} )
设置断点,调试运行
自动弹出ImageWatch窗口