0

So, I am trying to make an app where I need to have a space, a box, in which I have multiple possible combinations of the catan cards, they should be listed there for every player and you should be able to scroll through them, in that box. I am just starting out with QT and I am struggling really hard, don't recommend it to anyone. Anyway, this is the code:

        box = new QGroupBox(this);
        box->setAlignment(Qt::AlignLeft);
        box->setMinimumSize(450, 460);
        box->move(10, 30);
        box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        
        boxScroll = new QScrollArea(this);
        boxScroll->setWidget(box);
        boxScroll->move(10, 30);
        boxScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        boxScroll->setMinimumSize(450, 460);
    
        playersLayout = new QVBoxLayout(this);
        playersLayout->setSpacing(5);
        box->setLayout(playersLayout);
    
        players.push_back(Player("gigi"));
        players.push_back(Player("marcel"));
        players.push_back(Player("costel"));
    
        for (int i = 0; i < 5; i++)
            players[0].addCombination(Resources(i, i, i, i, i));
        for (int i = 0; i < 15; i++)
            players[1].addCombination(Resources(i, i, i, i, i));
    
        for (auto var : players)
        {
            QLabel* l = new QLabel(QString::fromStdString(var.name), this);
            l->setFont(QFont("Arial Black", 10));
            l->setAlignment(Qt::AlignLeft);
            l->setWordWrap(true);
            playersLayout->addWidget(l);
            for (auto& combo : var.resourceCombinations)
                playersLayout->addLayout(getCombination(combo));
        }

And this is the function:

QHBoxLayout* widget::getCombination(Resources res)
{
   QHBoxLayout* layout = new QHBoxLayout(this);
   //layout->setAlignment(Qt::AlignCenter);

   QLabel* nums[5], * imgs[5];
   for (int i = 0; i < 5; i++)
   {
       nums[i] = new QLabel(QString::number(res[i]), this);
       nums[i]->setFont(QFont("Arial Black", 10));
       nums[i]->setWordWrap(true);

       imgs[i] = new QLabel(this);
       imgs[i]->setWordWrap(true);
   }
   imgs[0]->setPixmap(QPixmap(":/widget/Images/lumber.png"));
   imgs[1]->setPixmap(QPixmap(":/widget/Images/brick.png"));
   imgs[2]->setPixmap(QPixmap(":/widget/Images/wool.png"));
   imgs[3]->setPixmap(QPixmap(":/widget/Images/grain.png"));
   imgs[4]->setPixmap(QPixmap(":/widget/Images/ore.png"));
   for (int i = 0; i < 5; i++)
   {
       imgs[i]->setScaledContents(true);
       layout->addWidget(nums[i]);
       layout->addWidget(imgs[i]);
   }

   return layout;
}

And this is the result, the box in the left: App Preview

The information is just getting crammed in there, the images are not keeping their aspect ratio and overall does not work, I tried setting spacing, contents margin, something with size policy, and probably a lot more, I even asked chatGPT a bunch of stuff but I am getting nowhere, also, can't find any good, in depth, tutorial for QT..

Miximan
  • 41
  • 3

1 Answers1

0

I think you need to call

box->setLayout(playersLayout);

before you call

boxScroll->setWidget(box);

See documentation: https://doc.qt.io/qt-6/qscrollarea.html#setWidget

It seems that doesn't solve the problem, but still the order of these two calls is important.