Modifying widgets in constructor

  • I have run into an issue where I try to change some properties for a widget or two (created in Designer) in their parent's constructor and they do not take effect.

    For instance, I have a QwtPlot (part of the Qwt graph package) added to my QMainWindow in designer and I try to change the plot's axis fonts in the QMainWindow's constructor with plot.setAxisFont(int axis, QFont font) but they do not change. Adding an update() or repaint() call after setAxisFont didn't help. The first call in the QMainWindow constructor is setupUi(this) so the QwtPlot widget should be initialized.

    If I wait until the QMainWindow is fully instantiated (e.g. constructor returns) and then call a method which sets the fonts (with the same code) the changes show. I seem to remember having this problem before with a Qt widget (not from Qwt) as well. Anyone know anything about this?

  • Hi!

    Can you print the source of your constructor method ?

  • Sure, here is what I was trying to do:

    @ImageMainWindow::ImageMainWindow(QSharedPointer<ConfigParams> config_params, QMenuBar *main_win_menubar, QWidget *parent) : QMainWindow(parent), ui(new Ui::ImageMainWindow) {

    //plot_title_font and plot_axis_font are QFonts
    plot_title_font_ = ui->histo_plot->font(); //histo_plot is a QwtPlot
    plot_title_font_.setPointSize(PLOT_TITLE_FONT_SIZE); //PLOT_TITLE_FONT_SIZE is const int 8

    plot_axis_font_ = plot_title_font_;

    ui->histo_plot->setAxisFont(QwtPlot::xBottom, plot_axis_font_);
    ui->histo_plot->setAxisFont(QwtPlot::yLeft, plot_axis_font_);

    and moving it to the showEvent method as follows works:

    @void ImageMainWindow::slt_set_plot_fonts() {
    //plot_axis_font setup in constructor
    ui->histo_plot->setAxisFont(QwtPlot::xBottom, plot_axis_font_);
    ui->histo_plot->setAxisFont(QwtPlot::yLeft, plot_axis_font_);

    void ImageMainWindow::showEvent(QShowEvent *event) {
    static bool been_here = false;

    if (!been_here) {
    been_here = true;


  • Sounds like a problem in Qwt to me, to be honest. I think what you were doing in the constructor should work. The only thing I can think about, is that perhaps (speculation) Qwt does not accept setting up axis before you actually told it about the kind of data is need to plot (and what axis there are in that)?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.