Qt入门

来源:互联网 发布:excel筛选重复数据删除 编辑:程序博客网 时间:2024/05/12 00:00

假如您有兴趣,能够用 man convert 来看看 convert 的周详内容.这里我只介绍假如把一个图片文
件转换成 xpm 的格式.我选择的是 Linux 中的代表图片.一苹可爱的小企鹅--logo.gif,相信每一个
安装了 linux 的人,应该都能够找到这个图片吧.您能够试这用 locate logo.gif 来寻找.找到以後,
把 logo.gif 复制到程式放有 form1 程式码的目录下. 然後用下面的指令来作图片的格式转换∶
convert logo.gif logo.xpm
这时侯,我们就能够看到目录中多了一个 logo.xpm 的文档了.


现在我们回到我们的 form1 程式码中.在前面加入
#include

然後在 TextLabel的後面任何地方,加入下面三行程式码∶

QPixmap pixmap("logo.xpm"); //这里就是告诉 pixmap 我们刚刚 convert 出来的那个图片
TextLabel->setPixmap(pixmap); //让 TextLabel 调用/显示 pixmap (logo.xpm)
TextLabel->setScaledContents( TRUE ) ; //打开自动平衡收放图片大小的功能

记住喔,一定不能在 TextLabel = new QLabel( this, "TextLabel1" ); 以前加入喔!
这时侯重新编译我们的程式码,在新的程式中,您就能够看到那苹可爱的小企鹅了.您也许
会发现企鹅胖的有点变形.那是因为自动平衡是根据您的 TextLabel 的大小来收放图片的.
所以我们只要把:
TextLabel1->setGeometry( QRect( 0, 0, 171, 91 ) );
改换成∶
TextLabel->setGeometry( QRect( 0, 0, 171, 191 ) );
也就是说把高度增加 100,重新编译以後.能够看到,企鹅的大小变得正常多了.

√ Label 也是最常用的组件.通常被用为在视窗中的文字显示.

RadioButton 的简介:

接下来让我慢看看 RadioButton. 在 QT Designer 中,有一个圆圈圈的图案,在圆圈中
有个黑色的实心圆圈.那个就是 RadioButton 了.现在我们在我们的 form1 中,增加一个 RadioButton.
然後来观察程式码.

在 .h 文档中,多出了两行

class QRadioButton; //告诉程式我们需要用到 QRadioButton

QRadioButton* RadioButton1; //QRadioButton *RadioButton1

在 .cpp 的文档中出现了下面三行程式码,连同一个 include 文档

#include //QRadioButton 所需要的 include 文档
RadioButton1 = new QRadioButton( this, "RadioButton1" ); //生成一个新的 QRadioButton 在介面 this上
// this 就是只当前的主介面.也就是我们的
// Form1 : public QDialog
// 白话文的方法来说呢,就是我们这个程式的
// 主显示视窗.相信大家已定很熟习了吧.

RadioButton1->setGeometry( QRect( 260, 60, 151, 61 ) ); // 通过 setGeometry(QRect()) 来设定我们这个
// RadioButton 的大小
RadioButton1->setText( tr( "RadioButton1" ) ); // 用 setText() 来给我们的这个 RadioButton
// 命名.这里这里名为 "RadioButton1"

将上面这些程式码加入到我们的 form1.h 和 form1.cpp 中,重新编译程式,我们就看到了一个名为 RadioButton1
的 RadioButton 了.

RadioButton 除了能够用到 PushButton 中的 setEnabled( FALSE )的选项来 Disable 这个 RadioButton
连同在 QT 中通用的 setFont 以外,还提共一个很常用的 setChecked (TRUE/FLASE) 功能.

QT Designer 中, 用 mouse click 一下我们刚刚生成的那个 RadioButton, 然後在 QT Designer 的
Property Edit 中.选择 checked, 在 checked 後面选择 True. 经过 uic 程式以後,重新观察 .cpp 程式
码, 您会发现,在 .cpp 文档中,多出了一行∶

RadioButton1->setChecked( TRUE ); //这行就设定了, RadioButton1 在程式一看使的时侯
//就已被设定为内定选项了.
重新编译程式码以後,就能够看出.这时侯 RadioButton1 前面已被选中了.

选择按钮,重要就在於选择.现在我们来增加另外一组 RadioButton 就能够很简洁的看出 setChecked 的作用了.
在您的 form1.h 文档中,加入∶
QRadioButton* RadioButton2;
在 form1.cpp 文档中加入∶
RadioButton2=new QRadioButton2(this,"RadioButton2");
RadioButton2->setGeometry(QRect(260, 140, 151, 61)); //这里的位置比 RadioButton1的位置低出
//80 来.
这时侯我们重新编译後,执行程式,就会发现. RadioButton1 前面是被自动选择上了.而 RadioButton2 则
没有.

因为 RadioButton 常常被用作二选一,三选一或多选一.所以这里向大家简单的介绍一下ButtonGroup.

QT Designer 中,上面那一堆小按钮中有一个是 ButtonGroup 的按钮.现在在这个上面用 mouse click
一下.然後在 QT Designer 中画出一个方的范围来. 这个范围要把刚刚我们画的那个 RadioButton 包进去
喔.一定要. (see figure 2)


figure 2


回来检查 uic 生成的 source code. 我们会发现
form1.h 中多出了
class QButtonGroup; // 用到 QButtonGroup
QButtonGroup* ButtonGroup1; // QButtonGroup *ButtonGroup1
而在 form1.cpp 中,有了

#include // QButtonGroup 需要的 .h 文档

ButtonGroup1 = new QButtonGroup( this, "ButtonGroup1" ); //在主视窗中生成 ButtonGroup1
ButtonGroup1->setGeometry( QRect( 230, 40, 251, 181 ) ); // 设定ButtonGroup的位置和大小
ButtonGroup1->setTitle( tr( "ButtonGroup1" ) ); //设定显示为 "ButtonGroup1"

原创粉丝点击