学习Qt过程中做的一些小demo,用书是Qt5.9 c++开发指南》.
Hello World.
- Label
- Push Button
- SIGNAL & SLOT
Simple editor.
- Check Box
- Radio Button
- Layout
- Text Edit: set font italic, set font bold, set font underline, set palette(to set textedit font color)
- SIGNAL & SLOT
- QObject::connect:
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
Text editor.
- Design Mode: 代码化+可视化(code+ui)
- Set icon
- ToorBar
- statusBar
- SIGNAL & SLOT
- Action
- qDebug()
- ProgressBar
- SpinBox
- Text Edit: set font, set font italic, set font bold, set font underline, set font pointsize, clear...
- QDir
- QFileDialog
- QMessageBox
- QFile + QIODevice & QFile + QTextStream to read and write text file
Simple price caculator.
- SpinBox(prefix, suffix)
- Group Box
- lineEdit
QString::number(), QString.setNum()
...
Simple Color Mixer
- Slider
- Dial
- QColor:
QColor color; color.setRgb(r, g, b, a);
- QPalette:
QPalette p = ui->textEdit->palette(); p.setColor(QPalette::Base, color); ui->textEdit->setPalette(p);
Use of time Classes.
- QTime
QTime t; t.start(); //do something t.elapsed();//do something time in miliseconds.
- QDateTime
QDateTime datetime = QDateTime::currentDateTime(); QString time_str = datetime.toString("yyyy-MM-dd hh:mm:ss")); QDateTime datetime = QDateTime::fromString(time_str,"yyyy-MM-dd hh:mm:ss");
- QTimer
QTimer* timer_1 = new QTimer(this); timer_1->stop(); timer_1->setInterval(1000); connect(timer_1, SIGNAL(timeout()), this, SLOT(on_timer_1_timeout())); void on_on_timer_1_timeout() { //do something every timer interval }
- Date/Time Edit
Use of item-based or model-view Widgets.
- Tool Box(Container)
- Tab Widget(Container)
- QListWidgetItem
- QListWidget(item based)
- QTableWidgetItem
- QTableWidget(item based), how to fill the table with items
- QStandardItemModel
- QTableView(Model-View), setModel
- QListWidget/QTableWidget: append, insert, delete(QTableWidget逆序遍历remove), clear, selectAll, selectNone, selectInvert, autoResize
- How to custom right-click Menu: 如何自定义右键快捷菜单
- Tool Button(How to bind action)
- Button setStyleSheet
- Read CSV(std ifstream, getline() split string)
- Container:QList, QStringList
- QDir:
//get dir_path files. QDir dir; dir.setPath(path); return dir.entryList(QDir::Files);
Use of QFileSystemModel to show the file-system.
- QFileSystemModel: setRootPath, index(QString path), size(), type(), filePath
- QTreeView/QTableView/QListView: setModel,setRootIndex
file_sys_model->setRootPath(path); qDebug()<<"file_sys_model root path:"<<file_sys_model->rootPath(); ui->treeView->setRootIndex(file_sys_model->index(path));//设置treeView根节点为选定目录 ui->tableView->setRootIndex(file_sys_model->index(path)); ui->tableView->resizeColumnsToContents(); ui->tableView->resizeRowsToContents(); ui->listView->setRootIndex(file_sys_model->index(path));
- QDesktopServices:
QDesktopServices::openUrl(QUrl(file_sys_model->filePath(index)));//listView item双击事件, 调用系统程序打开文件
Use of QStringListModel + QListView to show a csv file.
- QStringListModel: setStringList, insertRow(), setData(), removeRow(), removeRows(),index(int row)
- QModelIndex: QModelIndex.row()
- Bind the QStringListModel to QListView using QListView.setModel(), the QListView changes with the QStringListModel,
- QListView: currentIndex(), setCurrentIndex()
- QStandardItemModel
- Bind QStandardItemModel to QTableView
- QTextStream: read csv file
- CountryPopulation class
- QDataStream:
- export TableView data and read data to TableView using qt stream, override >> & << operator to read and write data from and to CountryPopulation objects.
- export TableView data and read data to TableView using binary format.
- Append/Insert/Delete/Modify the table, set the index on focus
- QFileDialog
- QMessageBox
- QFontDialog
- QColorDialog
- QInputDialog