QT做的小程序,挺好玩的,记录下过程: 先在QTcreator里面建个工程,QT GUI project。 建好之后,按我上一篇的设置里面稍作更改(参考hi.baidu.com/%C9%B3%BC%D3%BB%C6%BD%F0%CA%A5%B6%B7%CA%BF/blog/item/e7bb8dfaefeb9f12a8d3119f.html),要不总是提示崩溃的。 工程包含main.cpp ,temperdialog.cpp, temperdialog.h,temperdialog.ui四个文件。 下面一一贴上分别的文件里的代码: temperdialog.h: #ifndef TEMPERDIALOG_H #define TEMPERDIALOG_H #include <QDialog> #include "ui_temperdialog.h" namespace Ui { class temperDialog; } class temperDialog : public QDialog,public Ui::temperDialog { Q_OBJECT public: temperDialog(QWidget *parent = 0); //~temperDialog(); protected: void changeEvent(QEvent *e); private: Ui::temperDialog *ui; void initSettings(); void readSettings(); int currentsheshiwendu; int currenthuashiwendu; private slots: void huashiwendu(); void sheshiwendu(); void saveSettings(); }; #endif // TEMPERDIALOG_H temperdialog.cpp代码如下: #include "temperdialog.h" #include "ui_temperdialog.h" #include <QSettings> #include <QCoreApplication> #include <QApplication> /*temperDialog::temperDialog(QWidget *parent) : QDialog(parent), ui(new Ui::temperDialog) { ui->setupUi(this); } temperDialog::~temperDialog() { delete ui; }*/ void temperDialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void temperDialog::initSettings() { Sliderhuashi->setValue(currenthuashiwendu); dialsheshi->setValue(currentsheshiwendu); } void temperDialog::saveSettings() { currenthuashiwendu=Sliderhuashi->value(); currentsheshiwendu=dialsheshi->value(); QSettings settings; settings.setValue("Temperature/CelNumber",currentsheshiwendu); settings.setValue("Temperature/FahNumber",currenthuashiwendu); } void temperDialog::readSettings() { //下面的两句不可以设置删除,靠这两句存储设置,这里的设置可以让下次打开//软件后,保存上次设置的温度值 QCoreApplication::setOrganizationName("Mysoft"); QCoreApplication::setApplicationName("temperfile"); QSettings settings; // settings.setPath(QSettings::NativeFormat,QSettings::UserScope,"/home/yanwei/qt/temperature/"); currentsheshiwendu=settings.value("Temperature/CelNumber",0).toInt(); currenthuashiwendu=settings.value("Temperature/FahNumber",32).toInt(); } void temperDialog::huashiwendu() { Sliderhuashi->setValue(dialsheshi->value()*9/5+32); } void temperDialog::sheshiwendu() { dialsheshi->setValue((Sliderhuashi->value()-32)*5/9); } temperDialog::temperDialog(QWidget *parent) : QDialog(parent) { setupUi(this); readSettings(); initSettings(); } main.cpp代码如下: #include <QtGui/QApplication> #include "temperdialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); temperDialog w; w.show(); return a.exec(); } 最后运行界面如下: /************************************************************************ 以下是时钟程序,可以定时。最小化到系统托盘 /************************************************************************ 程序包含4个文件,ClcokWidget.ui,ClcokWidget.cpp, ClcokWidget.h和main.cpp。 下面一一贴上以上文件的代码 main.cpp: #include <QtGui/QApplication> #include "clockwidget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); clockWidget w; w.show(); return a.exec(); } ClcokWidget.cpp代码如下: #include "clockwidget.h" #include "ui_clockwidget.h" #include <QtGui> /*clockWidget::clockWidget(QWidget *parent) : QWidget(parent), ui(new Ui::clockWidget) { ui->setupUi(this); } clockWidget::~clockWidget() { delete ui; }*/ void clockWidget::changeEvent(QEvent *e) { QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void clockWidget::changetext() { labeltip->setText("TIME OUT!!!"); labeltip->show(); } void clockWidget::init() { labeltip->hide(); timer=new QTimer; //控制lcd的时间刷新 lcdNumber->display(QTime::currentTime().toString("hh:mm:ss")); itimer=new QTimer; /*以下红色部分代码为最小化到系统托盘的程序*/ trayIcon=new QSystemTrayIcon; trayIcon->setIcon(QIcon("./20101.ico")); trayIcon->show(); restoreAct=new QAction(QString::fromLocal8Bit("显示"),this); quitAct=new QAction(QString::fromLocal8Bit("退出"),this); trayIconMenu=new QMenu; trayIconMenu->addAction(restoreAct); trayIconMenu->addAction(quitAct); trayIcon->setContextMenu(trayIconMenu); } void clockWidget::con() { connect(timer,SIGNAL(timeout()),this,SLOT(showtime())); timer->start(1000); connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur())); itimer->start(20); connect(restoreAct,SIGNAL(triggered(bool)),this,SLOT(show())); connect (quitAct,SIGNAL(triggered(bool)),this,SLOT(close())); } void clockWidget::showtime() { lcdNumber->display(QTime::currentTime().toString("hh:mm:ss")); } void clockWidget::mini() { hide(); } void clockWidget::comToCur() { if( QVariant(QTime::currentTime()).toString() == QVariant(timeEdit->time()).toString() ) { //QApplication::beep(); emit isCurrentTime(); } } //void closeEvent(QCloseEvent * event); void clockWidget::closeEvent(QCloseEvent *event) { QMessageBox::information(0,"Quit","Are you sure to exit the Clock?"); } clockWidget::clockWidget(QWidget *parent) : QWidget(parent) { setupUi(this); init(); con(); } ClockWidget.h代码如下: #ifndef CLOCKWIDGET_H #define CLOCKWIDGET_H #include "ui_clockwidget.h" #include <QWidget> #include <QSystemTrayIcon> class QTimer; class QTime; class QVariant; class QAction; class QMenu; namespace Ui { class clockWidget; } class clockWidget : public QWidget ,public Ui::clockWidget { Q_OBJECT signals: void isCurrentTime(); public: clockWidget(QWidget *parent = 0); // ~clockWidget(); protected: void changeEvent(QEvent *e); void closeEvent(QCloseEvent *event); private: Ui::clockWidget *ui; void init(); void con(); QTimer * timer; QTimer *itimer; QAction *restoreAct; QAction *quitAct; QMenu *trayIconMenu; QSystemTrayIcon *trayIcon; private slots: void showtime(); void mini(); //最小化到托盘 void comToCur(); //比较所设置时间跟当前时间 void changetext(); }; #endif // CLOCKWIDGET_H 关键部分,应该是可以系统最小化托盘的那部分红色代码。 |
用户1770685 2014-6-26 22:23