-3

I have a form that was giving me a SegFault Exception so I broke it down and commented out everything but the mainwindow itself. Then I uncommented out the first 2 controls to display. I'm using a qgridlayout but it's not showing things correctly. When I show just the first item, it shows the label with the & instead of an underline - which makes sense since I don't show the "buddy". If I show both though or if I add the 'setBuddy' in then the buddy overwrites the label in the grid at 0,0. The label is supposed to be at 0,0 and the buddy/combo box is at 0, 1. I need to keep adding things in and my alternative is to use QHBoxLayouts repeatedly and I'm not nuts about that at all as things won't be aligned. Here's just the CPP code for the MainWindow to see:

QComboBox *MainWindow::getSourceComboBox()
{
    if(mCboSource == nullptr)
        {
        mCboSource = new QComboBox(this);

        mCboSource->addItems({{"URL", "Text", "Database"}});
        mCboSource->setCurrentIndex(0);
        mCboSource->setEditable(false);

        //connect(mCboSource, SIGNAL(QComboBox::currentTextChanged(QString &)), this, SLOT(MainWindow::sourceChanged(QString &)));
        }

    return(mCboSource);
}

QLabel *MainWindow::getSourceLabel()
{
    if(mLblSource == nullptr)
        {
        mLblSource = new QLabel("&Source:", this);
        mLblSource->setBuddy(getSourceComboBox());
        mLblSource->setAutoFillBackground(false);
        }

    return(mLblSource);
}

void MainWindow::initControls()
{
    QGridLayout *layout = new QGridLayout(this);

    layout->addWidget(this->getSourceLabel(), 0, 0, 1, 1);
    layout->addWidget(this->getSourceComboBox(), 0, 1, 1, 1);
    /*layout->addWidget(this->getConfigButton(), 0, 2);
    layout->addWidget(this->getDBPathLabel(), 1, 0);
    layout->addWidget(this->getDBPathText(), 1, 1);
    layout->addWidget(this->getBrowseButton(), 1, 2);
    layout->addWidget(this->getDataTable(), 2, 0, 1, 3);
    layout->addWidget(this->getButtonPanel(), 3, 0, 1, 3);*/

    setLayout(layout);
}

void MainWindow::initWindow()
{
    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));

    initControls();
}

MainWindow::MainWindow(QWidget *parent)
    :   QMainWindow(parent)
    ,   mCboSource(nullptr)
    //,   mLblDBPath(nullptr)
    ,   mLblSource(nullptr)
    /*,   mTxtDBPath(nullptr)
    ,   mBtnBrowse(nullptr)
    ,   mBtnConfig(nullptr)
    ,   mTblData(nullptr)*/
{
    initWindow();
}

MainWindow::~MainWindow()
{ }

But what could be wrong w/ the QT code???

Seth D. Fulmer
  • 490
  • 1
  • 4
  • 21
  • note that your last paragraph isnt sound. If you would be certain that the `const_cast` is fine then there would be no need for the method to be `const` in the first place. Either the const cast is wrong or you can remove the `const` from the method – 463035818_is_not_an_ai Aug 07 '21 at 15:33
  • It's const because it is not modifying the class in any way, shape, or form - I need to const_cast to allow within C++'s particular semantics that certain other methods and fields are accessed because THEY are not const - They're not const because in certain instances they do modify the class. Like for example perhaps I can take the const off initControls because for most of the times calling the getters for the control methods, they are creating the methods so altering the class but initWindow doesn't touch the class one bit. My point is that it's not affecting the QGridLayout. – Seth D. Fulmer Aug 07 '21 at 15:47
  • And from what I've always done and learned, you MUST create all controls as direct children of the window of which they are a control. You can't do: blah = new QComboBox(); - That is not done from all I've read especially since the MainWindow class will destroy the combo box at desructor time - You do that then you add it to a layout and set the layout and the mainwindow class will destroy the layout pointer as well. So that point made no sense. – Seth D. Fulmer Aug 07 '21 at 15:52
  • if it would not modify the object in any way, shape or form, then you would not need the const cast. Nevermind, you seem to reject any input anyhow – 463035818_is_not_an_ai Aug 07 '21 at 16:03
  • I removed the const and required changes regarding the const to see if it would change anything and I also noticed in my changes I was continuing to comment out the setLayout line so it wasn't even using GridLayout - But even with that line in place and the consts 'out', it does not function as expected. As I stated - it had nothing to do with the problem at hand. The code above has been changed as well to show. – Seth D. Fulmer Aug 07 '21 at 16:15
  • Please post a [mcve]. – 463035818_is_not_an_ai Aug 07 '21 at 16:33
  • I JUST did 463035818_is_not_a_number - That's the whole question - If you want me to post the App classes, header files, and the main.cpp file, I can make the question 200 pages long printed - but anyone can create an app class or include the main window class into the main.cpp if they want and reproduce this. – Seth D. Fulmer Aug 07 '21 at 16:39
  • Wow I wonder what you're doing because for me it shows the combo box and ONLY a combo box. – Seth D. Fulmer Aug 07 '21 at 16:45
  • https://imgur.com/a/YU9H401 – Seth D. Fulmer Aug 07 '21 at 16:52
  • a mcve is not your whole project. Rather it is minimal, ie there need not be code that is commented out, and reproducible, which this seems to be not. I don't know what more is needed or what not, but it seems like not everybody can add the missing pieces to reproduce your issue – 463035818_is_not_an_ai Aug 07 '21 at 16:53
  • Commented code is likewise - not needed - Anyone who is anyone knows this. I never remove code permanently from projects until I'm ready to commit my code to source control and say "I'm done with this" and even then really only when I'm rolling to production. I'm just going to have to resolve this myself because StackOverflow as usual is a waste of my time. – Seth D. Fulmer Aug 07 '21 at 17:07

1 Answers1

0

You are adding items almost as a central widget so you need to create a central widget and add the items and layout to that then add that central widget to the main window. That should solve the problem!

Seth D. Fulmer
  • 490
  • 1
  • 4
  • 21