[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Newbie: Why does KMainWindow::show() hang?!
From:       Felix <schmitt () voidcast ! de>
Date:       2005-04-26 1:46:10
Message-ID: 200504260346.10550.schmitt () voidcast ! de
[Download RAW message or body]

> > [see first in thread for full problem report]
> >
> > (...) KMainWindow::show() hangs with mem consumption skyrocketing +
> > cpu load at > 1.0 (...)
> > Could anyone give me a hint what the most common causes would be?
> >
> > (...) snippet of main.cpp:
> >
> > 023            kbinfmt *widget = new kbinfmt;
> > 024            widget->show();
>
> hard to say without knowing the kbinfmt class looks like =)

True(sorry).

The following sources are all pretty "generic", as created with kdepvelop.
 But I must have done sth to screw them up. The sources are complete, since I
 really don't know what's relevant (I don't reimplement any update(),
 repaint() or similar stuff!!), but without the (generic kdevelop) comments
 and without all the obvious includes for the sake of brevity, so it won't
 compile.

Please let me know whether you also need the kbinfmtIface and FileTable
sources.

kbinfmt.h:
=====================================================
#include "kbinfmtview.h"

class KToggleAction;
class KURL;

class kbinfmt : public KMainWindow
{
    Q_OBJECT
public:
    kbinfmt();

protected:
    virtual void dragEnterEvent(QDragEnterEvent *event);
    virtual void dropEvent(QDropEvent *event);

protected:
    void saveProperties(KConfig *);
    void readProperties(KConfig *);


private slots:
    void fileOpen();
    void fileSave();
    void fileSaveAs();
    void optionsShowToolbar();
    void optionsShowStatusbar();
    void optionsConfigureKeys();
    void optionsConfigureToolbars();
    void optionsPreferences();
    void editFind();
    void newToolbarConfig();
    void changeStatusbar(const QString& text);

private:
    void setupAccel();
    void setupActions();
    KURL fname;

private:
    kbinfmtView *m_view;
    KToggleAction *m_toolbarAction;
    KToggleAction *m_statusbarAction;
};

==============================================
==============================================

kbinfmt.cpp:
==============================================
#include "kbinfmt.h"

kbinfmt::kbinfmt()

        : KMainWindow( 0, "kbinfmt" ),

        m_view( new kbinfmtView( this ) ) {
    setAcceptDrops( true );

    setCentralWidget( m_view );

    setupActions();

    statusBar() ->show();

    setAutoSaveSettings();

    connect( m_view, SIGNAL( signalChangeStatusbar( const QString& ) ),
             this, SLOT( changeStatusbar( const QString& ) ) );
}

void kbinfmt::setupActions() {
    KStdAction::open( this, SLOT( fileOpen() ), actionCollection() );
    KStdAction::save( this, SLOT( fileSave() ), actionCollection() );
    KStdAction::saveAs( this, SLOT( fileSaveAs() ), actionCollection() );
    KStdAction::quit( kapp, SLOT( quit() ), actionCollection() );

    m_toolbarAction = KStdAction::showToolbar( this,
SLOT( optionsShowToolbar() ), actionCollection() );
    m_statusbarAction = KStdAction::showStatusbar( this,
SLOT( optionsShowStatusbar() ), actionCollection() );

    KStdAction::keyBindings( this, SLOT( optionsConfigureKeys() ),
actionCollection() );
    KStdAction::configureToolbars( this, SLOT( optionsConfigureToolbars() ),
actionCollection() );
    KStdAction::preferences( this, SLOT( optionsPreferences() ),
actionCollection() );
    KStdAction::find( this, SLOT( editFind() ), actionCollection() );

    KAction *custom = new KAction( i18n( "Cus&tom Menuitem" ), 0,
                                   this, SLOT( optionsPreferences() ),
                                   actionCollection(), "custom_action" );
    createGUI();
}

void kbinfmt::saveProperties( KConfig *config ) {
    if ( !fname.isEmpty() ) {
        config->writePathEntry( "lastURL", fname.path() );
    }
}

void kbinfmt::readProperties( KConfig *config ) {
    fname = config->readPathEntry( "lastURL" );
    if ( !fname.isEmpty() )
        m_view->open( fname );
}

void kbinfmt::dragEnterEvent( QDragEnterEvent *event ) {
    event->accept( KURLDrag::canDecode( event ) );
}

void kbinfmt::dropEvent( QDropEvent *event ) {
    KURL::List urls;
    if ( KURLDrag::decode( event, urls ) && !urls.isEmpty() ) {
        fname = urls.first();
        m_view->open(fname);
    }
}

void kbinfmt::fileOpen() {
    fname = KFileDialog::getOpenURL( QString::null, QString::null, this,
i18n( "Open Location" ) );
    if ( !fname.isEmpty() ) {
        setCaption( fname.prettyURL() );
        m_view->open( fname );
    } else
        setCaption( "" );
}

void kbinfmt::fileSave() {
    m_view->save(fname);
}

void kbinfmt::fileSaveAs() {
    fname = KFileDialog::getSaveURL();
    if ( !fname.isEmpty() && fname.isValid() )
        fileSave();
}

void kbinfmt::optionsShowToolbar() {
    if ( m_toolbarAction->isChecked() )
        toolBar() ->show();
    else
        toolBar() ->hide();
}

void kbinfmt::optionsShowStatusbar() {
    if ( m_statusbarAction->isChecked() )
        statusBar() ->show();
    else
        statusBar() ->hide();
}

void kbinfmt::optionsConfigureKeys() {
    KKeyDialog::configure( actionCollection() );
}

void kbinfmt::optionsConfigureToolbars() {
    saveMainWindowSettings( KGlobal::config(), autoSaveGroup() );
}

void kbinfmt::newToolbarConfig() {
    createGUI();
    applyMainWindowSettings( KGlobal::config(), autoSaveGroup() );
}

void kbinfmt::optionsPreferences() {
    kbinfmtPreferences dlg;
    if ( dlg.exec() ) {
        // redo your settings
    }
}

void kbinfmt::editFind() {
}

void kbinfmt::changeStatusbar( const QString& text ) {
    statusBar() ->message( text );
}

#include "kbinfmt.moc"
============================================
============================================

kbinfmtview.h:
============================================
#include "filetable.h"
class kbinfmtView : public QWidget, public kbinfmtIface
{
    Q_OBJECT
public:
    kbinfmtView(QWidget *parent);
    virtual ~kbinfmtView();
    virtual void open(const KURL& url);
    virtual void save(const KURL& url);
    virtual void find(QString s);

signals:
    void signalChangeStatusbar(const QString& text);

private:
    FileTable *fileTable;
    QTable *hexViewPos;
    QTable *asciiView;
    QCanvasRectangle * hexViewTop[2560];
    QCanvasRectangle * hexViewBottom[2560];
};
============================================
============================================

kbinfmtview.cpp:
============================================
#include "kbinfmtview.h"

kbinfmtView::kbinfmtView( QWidget *parent )

        : QWidget( parent )/*,

DCOPObject( "kbinfmtIface" )*/ {
    QGridLayout * top_layout = new QGridLayout( this, 3, 3 );
    fileTable = new FileTable( this );
    top_layout->addMultiCellWidget( fileTable, 1, 1, 0, 2 );
    top_layout->setRowStretch( 1, 1 );
    top_layout->setColStretch( 1, 1 );
}

kbinfmtView::~kbinfmtView() {}

void kbinfmtView::open( const KURL& url ) {
    char buf[ 10240 ];
    std::ifstream in( url.path(), std::ios::binary );
    std::vector<char> fcontent;
    int bufsize;
    std::cout << "reading " << url.path() << std::endl;
    while ( in.good() && ( bufsize = in.readsome( buf, 10240 ) ) > 0 ) {
        int size = fcontent.size();
        fcontent.resize( size + bufsize );
        for ( int i = 0; i < bufsize; i++ )
            fcontent[ i + size ] = buf[ i ];
        emit signalChangeStatusbar( QString("Reading... (") +
QString::number( size ) + " bytes)" );
        std::cout << "reading ... " << size << std::endl;
    }
    if (in.good())
        emit signalChangeStatusbar( "Done." );
	else
        emit signalChangeStatusbar( "Error reading file." );
    fileTable->setContents( fcontent );
}

void kbinfmtView::save( const KURL& url) {
    char buf[ 10240 ];
    std::ofstream out( url.path(), std::ios::binary );
    std::vector<char> fcontent = fileTable->getContents();
    int fcsize = fcontent.size();
    int idx = 0;
    while ( out.good() && (idx < fcsize)) {
        int i;
        for (int i = idx; (i < fcsize) && (i - idx < 10240); i++)
	    buf[i-idx] = fcontent[i];
	out.write(buf, i - idx);
	idx += i;
        emit signalChangeStatusbar( "Saving... (" + QString::number( idx ) +
 " bytes)" );
    }
    if (out.good())
        emit signalChangeStatusbar( "Done." );
	else
        emit signalChangeStatusbar( "Error writing file." );
    fileTable->setContents( fcontent );
}

void kbinfmtView::find(QString s) {
}

#include "kbinfmtview.moc"
==========================================
==========================================
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic