Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QBoxLayout is not shown properly



  • I tried to create a some textedit and pushbutton in layout without using UI . But i have received the plain latout at the End and i have these type of Error .

    QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
    QObject::connect: Parentheses expected, slot MainWindow::writetoFile in ../serial_mannualselct/mainwindow.cpp:34
    QObject::connect: (receiver name: 'MainWindow')

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setWindowTitle("TITLE");
        cBox  = new QComboBox;
        Save   = new QPushButton;
        SBox   = new QSpinBox;
        tEdit = new QTextEdit;
        Hlay  = new QHBoxLayout;
    
        cBox->addItem("First");
        cBox->addItem("Second");
        cBox->addItem("third");
        Save->setText("save");
    
        Hlay->addWidget(SBox);
        Hlay->addWidget(tEdit);
        Hlay->addWidget(cBox);
        Hlay->addWidget(Save);
        setLayout(Hlay);
    
        MainWindow::connect(Save, SIGNAL(clicked()), this, SLOT (writetoFile));// I think here there is a problem 
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::writetoFile(){
        QString fname = "/home/adx-soft1/Shankar/serial_mannualselct/file.txt";
        QFile file(fname);
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream stream(&file);
            QString textFromSpin = QString::number(SBox->value());
            QString textFromBox  = tEdit->toPlainText();
            QString textFromCombo= cBox->currentText();
           stream << "Spinner value"    << endl ;
           stream << "TEXT FROM BOX"    << endl ;
           stream << "TEXT FROM COMBO"  << endl 
    
    
        file.close();
        }
    
    

  • Lifetime Qt Champion

    Hi,

    QMainWindow already has a layout, the one that manages the docked widgets, tool bars, etc.

    Put your stuff inside a plain QWidget and use it as central widget.


  • Lifetime Qt Champion

    Hi,

    QMainWindow already has a layout, the one that manages the docked widgets, tool bars, etc.

    Put your stuff inside a plain QWidget and use it as central widget.


Log in to reply