QBoxLayout spacing

QBoxLayout spacing

本文关键字:spacing QBoxLayout      更新时间:2023-10-16

我有一个qboxlayout来显示数据库中的排行榜。我想这样对齐它们,但是似乎没有任何效果。我使用了固定标记,添加了空白空间,删除了间距和边距,并且在我不想要的地方之间仍然存在间距。

   PlayerList * playerList= new PlayerList();
   int xPos=this->width()/2 -200;
   int yPos=100;
   QFontDatabase::addApplicationFont(":/fonts/FredokaOne-Regular.ttf");
   QFont f("Fredoka One", 12, QFont::Normal);
   QSqlQuery query;
   query.exec(query);
   QVBoxLayout * layoutV=new QVBoxLayout;
   while(query.next()){
   Label1 = new QLabel;
   Label2 = new QLabel;
   Label3 = new QLabel;
   Label1->setFont(f);
   Label2->setFont(f);
   Label3->setFont(f);
   Label1->setStyleSheet("color: white");
   Label2->setStyleSheet("color: white");
   Label3->setStyleSheet("color: white");
   QHBoxLayout * layoutH= new QHBoxLayout;
   Label1->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Minimum);
   Label2->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Minimum);
   Label3->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Minimum);
   Label1->setText(query.value(0).toString());
   Label2->setText(query.value(1).toString());
   Label3->setText(query.value(2).toString());
   int val=0;
   QString yea = "";
   int text = 0;
   text = Label3->text().length();
   if(text<=16){
       val=16-text;
       for(int i=0;i<val;i++) yea+=" ";
   }
   Label3->setText(query.value(2).toString()+yea+"      ");
   yea = "";
   text = Label1->text().length();
   if(text<=16){
       val=16-text;
       for(int i=0;i<val;i++) yea+=" ";
   }
   Label1->setText(query.value(0).toString()+yea+"      ");
   yea="      ";
   qDebug() << Label1->text() << yea << Label3->text() << yea << Label2->text();
   layoutH->addStretch();
   layoutH->addWidget(Label1);
   layoutH->addWidget(Label3);
   layoutH->addWidget(Label2);
   layoutH->addStretch();
   layoutH->setSpacing(0);
   layoutV->addLayout(layoutH);
    } 
   playerList->setStyleSheet("background-color: black");
   playerList->setLayout(layoutV);
   scene->addWidget(playerList->scrollArea)->setPos(xPos,yPos);

https://imgur.com/2mm9rce这就是布局中的样子,这就是Qdebug中的样子:https://imgur.com/vndundz

对我来说很难猜测,这是您想要的理想状态。

绝对缺少的是通常的:

layout->addWidget(label);

这种方式label从现在开始,由layout

只有这样,值得考虑修剪太长的字符串等。