Qt 菜鸟经验总结18则

Qt version is 3.2.3 企业版

 

1.//可以直接在对话框上创建按钮等控件,看来布局类或者布局类物体只是 //用来方便我们放置控件的。

 

2.关于调用 moc 程序处理 signal and slot机制,有时候 qmake 生成的 Makefile 文件并不调用moc来预处理,因此这个时候可以先删除 Makefile,然后再生成。 错误信息大致为:

 

3.允许这样的语句:Layout->setGeometry( QRect( 10,10,100,50 ) ); QHBoxLayout等布局对象(but not widget )里的 Widget 的排列,是按其加入的先后顺序而定的。要让其显示在一个窗口上,需要把让这个窗口作为其 Parent.

 

 4.setMargin() sets the width of the outer border. This is the width of the reserved space along each of the QBoxLayout's four sides. 就是设置其周围的空白距离。

 

5.setSpacing() sets the width between neighboring boxes. (You can use addSpacing() to get more space at a particular spot. ) 就是设置相邻对象间的距离。

 

 

 6.addStretch() to create an empty, stretchable box. 相当于加入了一个空白的不显示的部件。

 

7.Qt程序的全屏幕显示: //全屏幕显示 //main_window->setGeometry( 0, 0, QApplication::desktop()->width(), QApplication::desktop()->height() ); //或者: main_window->resize( QApplication::desktop()->width(), QApplication::desktop()->height() ); 实际上只有第一种方法可以。第二种方法只是把窗口大小调整为屏幕大小,但是由于其显示位置未定,所以显示出来还是不行。第一种方法直接设置了窗口的显示位置为屏幕左上角。 Qapplication::desktop() 返回了一个 QdesktopWidget 的对象指针。 全屏幕显示后,windows下依然无法挡住任务栏。(为了实现跨平台性,最好还是用Qt提供的方法。例如这里用的就是 Qt的方法,而不是用的Windows API)

 

8.使用以下代码可以为一个窗口部件加入背景图片: QPixmap pic; pic.load( "qqpet.bmp" ); Label.setPixmap( pic ); Label.show(); QpushButton 也可以。但是在使用了 setPixmap 后,原来的文字就显示不了了。如果在setPixmap后设置文字,则图片就显示不了。 其他事项:the constructor of QPixmap() acept char * only for xpm image. hope file is placed proper and is bmp. jpg's gif's can cause error(configure).----不能缩放图象。

 

9.调用 void QWidget::setFocus () [virtual slot] 即可设置一个焦点到一个物体上。

 

10.让窗口保持固定大小: main_window->setMinimumSize( g_main_window_w, g_main_window_h ); main_window->setMaximumSize( g_main_window_w, g_main_window_h ); 只要让最小尺寸和最大尺寸相等即可。

 

 11.获得系统日期: QDate Date = QDate::currentDate(); int year = Date.year(); int month = Date.month(); int day = Date.day();

 

12.获得系统时间: QTime Time = QTime::currentTime(); int hour = Time.hour(); int minute = Time.minute(); int second = Time.second();

 

13.QString::number 可以直接传其一个数而返回一个 QString 对象。 因此可以用以下代码: m_textedit->setText( QString::number( 10 ) );

 

14.利用 QString::toInt() 之类的接口可以转换 字符串为数。这就可以把 QLineEdit之类返回的内容转换格式。 文档里的描述: int QString::toInt ( bool * ok = 0, int base = 10 ) const Returns the string converted to an int value to the base base, which is 10 by default and must be between 2 and 36. If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

 

15.关于 QTimer . 文档: QTimer is very easy to use: create a QTimer, call start() to start it and connect its timeout() to the appropriate slots. When the time is up it will emit the timeout() signal. Note that a QTimer object is destroyed automatically when its parent object is destroyed. 可以这样做: QTimer *time = new QTimer( this ); Timer->start( 1000, false ); //每1000ms timer-out一次,并一直工作(false ),为 true只工作一次。 Connect( timer, SIGNAL( timeout() ), this, SLOT( dealTimer() ) );

 

 16.关于QSpinBox: QSpinBox allows the user to choose a value either by clicking the up/down buttons to increase/decrease the value currently displayed or by typing the value directly into the spin box. If the value is entered directly into the spin box, Enter (or Return) must be pressed to apply the new value. The value is usually an integer. 如下方式创建: QSpinBox *spin_box = new QSpinBox( 0, 100, 1, main_window ); spin_box->setGeometry( 10,10, 20, 10 ); 这样创建后,它只允许输入数字,可以设置其几何大小。 使用int QSpinBox::value () const得到其当前值。

 

 17.Main 可以这样: clock->show(); int result = a.exec(); delete clock; return result;

 

18. Qt 中的中文:如果使程序只支持一种编码,也可以直接把整个应用程序的编码设置为GBK编码, 然后在字符串之前 加tr(QObject::tr), #include qApp->setDefaultCodec( QTextCodec::codecForName("GBK") ); QLabel *label = new QLabel( tr("中文标签") );

 

19. Qt显示中文最简单办法 QString str; str = QString::fromLocal8Bit("....."); QLabel tLabel(str, 0);

 

20, 去除标题栏和边框 :QWidget(parent, Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder)

 

21,修改程序主窗口标题 setWindowTitle(QString &); //Qt 4

 

22, 给Qt应用程序加图标 1,准备ico图标, 比如myappico.ico 2,建个rc文本文件名, 比如myrc.rc 在里面加入IDI_ICON1 ICON DISCARDABLE "myappico.ico" 3,在pro工程文件中加入 setWindowIcon(QIcon("myappico.ico")); //一般应该加到class::public QWdiget中.因为 setWindowIcon()是QWidget public function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值