简体   繁体   English

QDial:如何将 int 值传递给需要 const char* 的 Qfile?

[英]QDial: how to pass int value to a Qfile that requires const char*?

Problem is already described in title: I'm using a QDial to set contrast level of a LCD 7" display (connected to an embedded Linux Single Board Computer).问题已在标题中描述:我正在使用QDial设置 LCD 7" 显示器(连接到嵌入式 Linux 单板计算机)的对比度级别。

Simple way to do this is to send on terminal an "echo" instruction.这样做的简单方法是在终端上发送“回声”指令。 I send it as a QFile:我将它作为 QFile 发送:

QFile ContrLCD("/sys/class/backlight/backlight/brightness");

and I need to send the level writing in this QFile我需要在这个QFile发送水平写作

ContrLCD.write("number");

Problem is QDial manage int variable, while QFile requires const char* to send number corresponding to contrast.问题是QDial管理int变量,而QFile需要const char*来发送与对比度对应的数字。

How to do this?这该怎么做?

BacklightController::setBrightness(int brightness) // slot
{
    QFile f{"/sys/class/backlight/backlight/brightness"};
    if (!f.open(QIODevice::WriteOnly)) return;
    QTextStream stream{&f};
    stream << brightness;
}

This example is taken from the documentation for QFile :此示例取自QFile的文档:

 QFile file("out.txt"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out(&file); out << "The magic number is: " << 49 << "\\n";

Seems like you can use a QTextStream to write formatted output to a QFile .似乎您可以使用QTextStream将格式化输出写入QFile I did not use it myself, so there might be other ways, but for formatted output of numbers this seems to be the right tool.我自己没有使用它,所以可能还有其他方法,但是对于数字的格式化输出,这似乎是正确的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM