您的位置:首页 > 编程语言 > Qt开发

Qt学习笔记之QComboBox、QFontComboBox、QSpinBox

2017-08-07 21:12 197 查看
QComboBox 下拉列表框

#include <QApplication>
#include <QComboBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QComboBox *comboBox;
//实例 QComboBox
comboBox = new QComboBox();
//控件显示位置大小
comboBox->setGeometry(QRect(50,50,120,25));
//定义字符串列表
QStringList str;
str << "数学" << "语文" << "地理";
//将字符串列表绑定 QComboBox 控件
comboBox->addItems(str);
comboBox->show();
return a.exec();
}


QFontComboBox 字体下拉列表框

main.cpp

#include "mainwindow.H"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow m;
m.show();
return a.exec();
}


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFontComboBox>
#include <QPushButton>
#include <QLabel>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
QFontComboBox *fontComboBox;
QPushButton *button;
QLabel *label;
private slots:
void textButton();
};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.H"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//实例 QFontComboBox
fontComboBox = new QFontComboBox(this);
//实例 QPushButton
button = new QPushButton(this);
//实例 QLabel
label = new QLabel(this);
label->setGeometry(QRect(50,150,300,25));
//按钮名
button->setText("按钮");
//位置
button->move(180,50);
//事件
connect(button,SIGNAL(released()),this,SLOT(textButton()));
//QFontComboBox 控件位置
fontComboBox->setGeometry(QRect(50,50,120,25));
}
void MainWindow::textButton()
{
label->setText("选择字体:"+fontComboBox->currentText());
}
MainWindow::~MainWindow()
{
delete ui;
}


QSpinBox 控件

#include <QApplication>
#include <QSpinBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSpinBox *spinBox;
//实例 QSpinBox
spinBox = new QSpinBox();
//位置
spinBox->setGeometry(QRect(50,50,100,25));
//值范围
spinBox->setRange(0,200);
//初始值
spinBox->setValue(10);
//后缀
spinBox->setSuffix("元");
//前缀
spinBox->setPrefix("$");
spinBox->show();
return a.exec();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: