Qt入门-QLabel类

来源:互联网 发布:在淘宝买手机好吗 编辑:程序博客网 时间:2024/04/28 04:34

    QLabel是QT界面中的标签类,它从QFrame下继承:

class Q_GUI_EXPORT QLabel : public QFrame{    Q_OBJECT


定义一个QLable类:

QLabel *label = new QLabel(this);


设置它的外观、文字、对齐方式:

 label->setFrameStyle(QFrame::Panel | QFrame::Sunken); label->setText("first line\nsecond line"); label->setAlignment(Qt::AlignBottom | Qt::AlignRight);


通过使用“&”字符,可以设置控件的快捷键,如:

 QLineEdit* phoneEdit = new QLineEdit(this); QLabel* phoneLabel = new QLabel("&Phone:", this); phoneLabel->setBuddy(phoneEdit);

 

则可以通过“ALT+P”激活phoneEdit。


示例:

#include <QApplication>#include <QMainWindow.h>#include <QLabel.h>#include <QRect.h>#include <QFont.h>int main(int argc, char *argv[]){QApplication a(argc, argv);QMainWindow *mainWindow = new QMainWindow;QLabel *lbl = new QLabel(mainWindow);QFont lbl_font;lbl_font.setPointSize(16);      //设置字体大小lbl->setFont(lbl_font);lbl->setText("Hello World.");lbl->setGeometry(QRect(20, 20, 150, 30)); //设置大小和位置lbl->setFrameStyle(QFrame::Panel | QFrame::Sunken); //设置外观mainWindow->resize(200, 100);   //设置主窗体大小mainWindow->setWindowTitle("Qt Test");  //设置主窗体标签mainWindow->show();return a.exec();}


 

原创粉丝点击