qt some datastrcut
海前 王
Posted on August 23, 2024
#if 0
#include <QCoreApplication>
#include <QByteArray>
#include <QDebug>
#include <vector>
#include <cstring> // For memcpy
#include<iterator>
#include<QMap>
#include<QColor>
#include<QMapIterator>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// float 转 QByteArray
float fn = 10000.0;
QByteArray qba_f;
// 确保 QByteArray 大小足够存储 float 数据
qba_f.resize(sizeof(float));
// 将 float 数据复制到 QByteArray 中
memcpy(qba_f.data(), &fn, sizeof(float));
// 将 QByteArray 转换成 std::vector<uint8_t>
std::vector<uint8_t> vecf(qba_f.begin(), qba_f.end());
// 打印 std::vector<uint8_t> 的内容
for (uint8_t byte : vecf) {
qDebug() << static_cast<int>(byte); // 打印每个字节的十进制值
}
QByteArray arr="你好";
// arr.setNum(12);
arr.push_back("c");
qDebug()<<arr;
arr.simplified();
qDebug()<<arr;
QMap<int,std::string> qmap;
qmap.insert(1,"dd");
qmap.insert(11,"dgd"); qmap.insert(14,"ddd"); qmap.insert(15,"ddf");
for(auto i:qmap){
qDebug()<<i.data();
}
// 使用 QMap 的迭代器进行遍历
for (auto it = qmap.begin(); it != qmap.end(); ++it) {
// qDebug() << "Key:" << it.key() << "Value:" << it.value();
}
QColor q;
return a.exec();
}
#endif
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QSpinBox>
#include <QVBoxLayout>
#include <QDebug>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QWidget(parent)
{
QSpinBox *spinBox = new QSpinBox(this);
QPushButton *button = new QPushButton("Print SpinBox Value", this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(spinBox);
layout->addWidget(button);
connect(button, &QPushButton::clicked, this, &MainWindow::handleButtonClicked);
}
private slots:
void handleButtonClicked()
{
// 使用 qobject_cast 将 sender() 转换为 QSpinBox*
QSpinBox *spinBox = qobject_cast<QSpinBox *>(sender());
if (spinBox) {
// 如果转换成功,输出 QSpinBox 的值
qDebug() << "SpinBox value:" << spinBox->value();
} else {
qDebug() << "Sender is not a QSpinBox";
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
💖 💪 🙅 🚩
海前 王
Posted on August 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024