我用Qt创建了一些GUI应用程序.我的GUI应用程序包含按钮和单选按钮等控件.当我运行应用程序时,按钮内的按钮和字体看起来很正常.当我将显示器的DPI缩放尺寸从100%更改为150%或200%时,控件的字体大小变大但控制尺寸(按钮,单选按钮)无论分辨率如何.因此,内部控件中的文本被切断.请参阅附图.
当DPI缩放大小设置为100%时,Qt应用程序看起来

当DPI缩放大小设置为200%时,Qt应用程序看起来

我也在一些平板电脑上运行我的应用程序.在平板电脑中,DPI标度值应该超过150%,否则一切都将显示得非常小.
我在网上搜索了在Qt中创建UI应用程序,无论分辨率和DPI比例值如何,但没有运气.所以我在这里发布我的任务.如果有办法摆脱这种情况,请告诉我.
我需要在QT中获得显示的DPI值.我可以使用以下内容在QT 5.0中获取它:
#include <QScreen>
......
......
QScreen *srn = QApplication::screens().at(0);
qreal dotsPerInch = (qreal)srn->logicalDotsPerInch();
Run Code Online (Sandbox Code Playgroud)
但是相同的代码在QT 4.x版本中引发错误.我的项目是在QT 4.x版本中开发的.所以我需要在QT 4.x版本中等效上面的代码.
我想检查向量中是否存在元素。我知道下面的代码将对其进行检查。
#include <algorithm>
if ( std::find(vector.begin(), vector.end(), item) != vector.end() )
std::cout << "found";
else
std::cout << "not found";
Run Code Online (Sandbox Code Playgroud)
但是我有任何类型的向量。即std::vector<std::any>
我将元素推入这样的向量中。
std::vector<std::any> temp;
temp.emplace_back(std::string("A"));
temp.emplace_back(10);
temp.emplace_back(3.14f);
Run Code Online (Sandbox Code Playgroud)
因此,我需要查找向量中是否存在字符串“ A”。std :: find可以在这里帮助吗?
截至目前,我正在使用下面的代码来做到这一点
bool isItemPresentInAnyVector(std::vector<std::any> items, std::any item)
{
for (const auto& it : items)
{
if (it.type() == typeid(std::string) && item.type() == typeid(std::string))
{
std::string strVecItem = std::any_cast<std::string>(it);
std::string strItem = std::any_cast<std::string>(item);
if (strVecItem.compare(strItem) == 0)
return true;
}
else if (it.type() == typeid(int) && item.type() == typeid(int))
{
int …Run Code Online (Sandbox Code Playgroud) 请考虑以下函数定义
void fn(int a, int b)
{
//...
//...
}
void fn(int a, int b, int c = 0)
{
//...
//...
}
Run Code Online (Sandbox Code Playgroud)
在main函数中,我使用2个参数调用fn:
int main()
{
fn(10, 15);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以想知道编译器如何处理这种情况.
我正在使用以下代码来检查文件是否存在或不使用std::ifstream.
#include <iostream>
#include <fstream>
int main()
{
if (std::ifstream("file.txt"))
{
std::cout << "File present\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于我没有使用任何对象std::ifstream,我该如何关闭文件?还是不需要调用close?
我有2个功能。第一个功能是以写入模式打开一个文件并向其中写入一些内容,然后关闭它。
FILE *fp = fopen("file.txt", "w");
//writing itnot file using fwrite
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
第二个函数以读取模式打开文件,解析内容,然后关闭文件。
FILE *fp = fopen("file.txt", "r");
//parsing logic
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
在 中main,我依次调用function1和function2。
int main()
{
function1();
function2();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
有时,function1 fopen会失败并显示错误号 13,即权限被拒绝。我只是有时观察到这一点。我在 2 秒后引入了一个sleepin ,它开始正常工作,没有任何问题。function1fclose
所以我怀疑文件没有立即发布fclose。睡眠并不是正确的解决办法。谁能建议如何解决这个问题?我在这里给出的示例是一个用例,实际代码在线程环境中运行。
我的应用程序中有类似的结构.
typedef struct _Test
{
int n;
struct Test *Next;
}Test;
int main(int argc, char **argv)
{
Test tmp, tmp1;
tmp.n = 1;
tmp.Next = NULL;
tmp1.n = 0;
tmp1.Next = &tmp;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在线
tmp1.Next = &tmp;
Run Code Online (Sandbox Code Playgroud)
我收到以下警告消息:警告C4133:'=':不兼容的类型 - 从'Test*'到'Test*'
上面的代码有什么问题?
我有一个结构,需要初始化它的对象std::fill.
typedef struct _test
{
char name[32];
char key[4];
int count;
}test;
Run Code Online (Sandbox Code Playgroud)
截至目前,我正在使用memset.我需要改成它std::fill.我在下面尝试过,但std::fill抛出了结构对象的编译器错误.
test t;
char a[5];
std::fill(a, a + 5, 0);
std::fill(t, sizeof(t), 0);
Run Code Online (Sandbox Code Playgroud)
注意:我不想用这种方式初始化. char a[5] = {0};
我是QT的新手.我在浮点值方面遇到了一些奇怪的问题.以下代码显示带小数点的消息框.即10.53
QMessageBox Msgbox;
float num = 10.53;
QString str = QString::number(num, 'g', 4);
Msgbox.setText(str);
Msgbox.exec();
Run Code Online (Sandbox Code Playgroud)
以下代码显示一个没有小数点的消息框.即,1
QMessageBox Msgbox;
float num = 120/77;
QString str = QString::number(num, 'g', 4);
Msgbox.setText(str);
Msgbox.exec();
Run Code Online (Sandbox Code Playgroud)
为什么在第二个代码片段中忽略小数点后的数字?我将数据类型更改为double和qreal.没有任何效果.
我需要在控制台中打印十六进制和十进制值。我用下面的一段代码来做到这一点。
std::stringstream ss;
ss << "0x" << std::uppercase << std::hex << 15;
std::cout << ss.str() << std::endl;
ss.str("");
ss.clear();
ss << 15;
std::cout << ss.str() << std::endl;
Run Code Online (Sandbox Code Playgroud)
但我以十六进制格式获取这两个值。如何重置字符串流?
c++ ×6
qt ×3
c ×2
c++17 ×1
dpi ×1
fclose ×1
fopen ×1
ifstream ×1
oop ×1
overloading ×1
resolution ×1
std ×1
stdany ×1
stringstream ×1
struct ×1