Position a QDialog at the bottom of its parent

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 123
Joined: Thu Jun 11 2020 12:18 pm

Position a QDialog at the bottom of its parent

Post by seasoned_geek »

I appear to be suffering from severe head-up-butt syndrome right now. I just wanted to put the input dialog for EDT responses at the bottom of the tabbed widget. I swear I've done this in Qt before. There seems to be something forcing the dialog back to center no matter what I do.

https://www.logikalsolutions.com/wordpress/wp-content/uploads/2020/08/dialog-issue.png

Every trick I could find online has also failed.

I'm trying to avoid hand rolling a dialog-like frameless window possessing its own event loop, but that may be what I have to do.

Code: Select all

/**************************************************************************
*
* Copyright (c) 2020 Roland Hughes
*
* Diamond Editor is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* Diamond is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
***************************************************************************/
#ifndef DIALOGEDTPROMPT_H
#define DIALOGEDTPROMPT_H

#include "edt_lineedit.h"

#include <QDialog>
#include <QLabel>
#include <QVBoxLayout>


class Dialog_Edt_Prompt : public QDialog
{
    CS_OBJECT( Dialog_Edt_Prompt )

public:
    Dialog_Edt_Prompt( QString labelText, bool allowDirection, QWidget *parent = nullptr );

    int terminator()            { return m_lineEdit->terminator();}
    bool ctrlMSubstitution()    { return m_lineEdit->ctrlMSubstitution();}
    bool allowDirection()       { return m_lineEdit->allowDirection();}
    QString text()              { return m_lineEdit->text();}

    void setLabelPalette( QPalette &p )  { m_label->setPalette( p );}
    void setEditPalette( QPalette &p )   { m_lineEdit->setPalette( p );}
    void setText( QString txt )          { m_lineEdit->setText( txt );}

    CS_SLOT_1( Public, void reposition() )
    CS_SLOT_2( reposition )

protected:
    void showEvent( QShowEvent *e );

private:
    Edt_LineEdit *m_lineEdit;
    QLabel       *m_label;
};

#endif

Code: Select all

/**************************************************************************
*
* Copyright (c) 2020 Roland Hughes
*
* Diamond Editor is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* Diamond is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
***************************************************************************/

#include "dialog_edt_prompt.h"
#include "overlord.h"
#include <QStyle>

Dialog_Edt_Prompt::Dialog_Edt_Prompt( QString labelText, bool allowDirection, QWidget *parent ) :
    QDialog( parent )
    , m_lineEdit( nullptr )
    , m_label( nullptr )
{

    Qt::WindowFlags flags = this->windowFlags();
    // Qt::CustomizeWindowHint
    // Qt::FramelessWindowHint
    setWindowFlags( flags | Qt::FramelessWindowHint );

    //setSizeGripEnabled( false );
    //setModal(false);

    QPalette temp = palette();
    temp.setColor( this->foregroundRole(), Overlord::getInstance()->currentTheme().colorText() );
    temp.setColor( this->backgroundRole(), Overlord::getInstance()->currentTheme().colorBack() );
    setPalette( temp );

    m_label = new QLabel( labelText, this );
    m_lineEdit = new Edt_LineEdit( this );
    m_lineEdit->set_allowDirection( allowDirection );

    temp = m_label->palette();
    temp.setColor( QPalette::Text, Overlord::getInstance()->currentTheme().syntaxConstant().color() );

    connect( m_lineEdit, &Edt_LineEdit::inputComplete, this, &QDialog::accept );

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget( m_label );
    layout->addWidget( m_lineEdit );
    setLayout( layout );

    if ( parentWidget() )
    {
        setMinimumWidth( parentWidget()->width() );
    }
}

void Dialog_Edt_Prompt::reposition()
{
    if ( parentWidget() )
    {
        qDebug() << "reposition()";
        QSize parentSize = parentWidget()->size();
        int w = size().width();
        int h = size().height();
        setGeometry( 0, ( parentSize.height() - h ), w, h );
        qDebug() << "dialog bottomLeft: " << rect().bottomLeft();
    }
}

void Dialog_Edt_Prompt::showEvent( QShowEvent *e )
{
    if ( parent() )
    {
        qDebug() << "showEvent()";

#if 0
        qDebug() << "bottomLeft before: " << rect().bottomLeft();
        QRect parentRect( parentWidget()->mapToGlobal( QPoint( 0, 0 ) ), parentWidget()->size() );
        qDebug() << "parent rec bottomLeft: " << parentRect.bottomLeft();
        rect().moveBottomLeft( parentRect.bottomLeft() );
        qDebug() << "bottomLeft after: " << rect().bottomLeft();
#endif

        QSize parentSize = parentWidget()->size();
        int w = size().width();
        int h = size().height();
        setGeometry( 0, ( parentSize.height() - h ), w, h );
        qDebug() << "dialog bottomLeft: " << rect().bottomLeft();

    }
    else
    {
        QDialog::showEvent( e );
    }
}
There has got to be something stupid simple that I just missed.

A swift boot to the head would be appreciated.

Post Reply