目录
前言
人都是有两面性的,当你有价值的时候(入了别人眼的时候),就会得到不同的待遇!所以,没有入别人眼的时候,自然就不会被认真对待!努力,学习,人真的可以选择自己的层次!
制作自定义的窗口部件-继承Qt已有的窗口部件
- 在项目上右键添加新文件,然后建立里一个
hexspinbox类,就会自动在头文件目录和源文件目录生成hexspinbox.cpp和hexspinbox.h。
Qt代码
#include "dialog.h"
#include <QApplication>
#include "hexspinbox.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Dialog w;
// w.show();
HexSpinBox h;
h.show();
return a.exec();
}
- hexspinbox.h
#ifndef HEXSPINBOX_H
#define HEXSPINBOX_H
#include <QSpinBox>
class QRegExpValidator;
class HexSpinBox : public QSpinBox
{
public:
HexSpinBox(QWidget* parent = 0);
protected:
QValidator::State validate(QString &input, int &pos) const;
int valueFromText(const QString &text) const;
QString textFromValue(int val) const;
private:
QRegExpValidator *validator;
};
#endif // HEXSPINBOX_H
- hexspinbox.cpp
#include "hexspinbox.h"
#include <QtWidgets>
HexSpinBox::HexSpinBox(QWidget* parent):QSpinBox(parent)
{
setRange(0, 255);
validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);
}
// when we input a value, validate() will be called automatically
// To decide whether the value is good
QValidator::State HexSpinBox::validate(QString &input, int &pos) const
{
return validator->validate(input, pos);
}
// when the value is going to be show, we should transform it
// into hex, also you can make it Upper
QString HexSpinBox::textFromValue(int value) const
{
return QString::number(value, 16).toUpper();
}
// Qt will save the value as Int. so when we input a hex
// Qt will call valueFromText to transform hex to Int
int HexSpinBox::valueFromText(const QString &text) const
{
bool ok;
return text.toInt(&ok, 16);
}
总结
这个程序使用了C++的继承,感觉学习Qt就是学习别人是怎么设计类的,设计了那些方法和虚函数,然后使用别人的接口实现自己的功能。感谢提供别人录制的视频,提供了代码!永远学习,总会有不一样的那一天!
5179



被折叠的 条评论
为什么被折叠?



